Hi
I have written a program that gets data from hardware over the serial port. The data varies from 15 seconds to 5 hours worth.
I work out how many milliseconds the thread.sleep needs to be with a while loop and update a progress bar.
The problem is that on some computers it works fine, but others it's so slow that the system hangs.
Is there a betterway to do this handling? I think that older and slower cpu's are the problem ans that the thread.sleep does not count in realtime any more.

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            this.Invoke(new EventHandler(DoUpdate));
        }

        private void DoUpdate(object s, EventArgs e)
        {
            ClearData();
            try
            {
                startValues1 = serialPort1.ReadTo("");
           
            
                String[] subData1 = startValues1.Split(new char[] { ' ' });
                double recordedTime = Convert.ToDouble(subData1[13]);
                int dataTime1 = Convert.ToInt32((recordedTime * 0.1) * 60);
                int dataTime = (dataTime1 * 10) + 2000;
                int n = 0;
                int value1 = 0;
                //Set the total number of steps to 100 (or 100%)
                progressBar1.Maximum = dataTime;
                progressBar1.Minimum = 0;
                //Set the initial value of the progress bar to 0% completed
                progressBar1.Step = 1;
                //If your progress bar is already visible you don't need this. But this is one of those objects I like to hide when I'm not using it
                progressBar1.Visible = true;
                //This next line tells your application to wait or go to sleep for 1000ms / 1 second
               
                while (n <= dataTime)
                {
                                     
                    System.Threading.Thread.Sleep(1);
                    n = n + 1;
                    progressBar1.Value = value1;
                    value1 = value1 + 1;
                }

                
            //System.Windows.Forms.MessageBox.Show("Download Complete!");

            
                PortData += serialPort1.ReadExisting();

Edited 7 Years Ago by procomp65: n/a

I don't understand what you're doing here. Why not just have a timer update update the progress bar and when the timer calculates 100% position then do whatever you need to after that.

how would I go about it. I cant seem to beable to connect the timer1_ticks to the progress bar.

How would I implement a timer to stop the thread? You have to stop the thread inorder to give the hardware device time to dump the data in the serial ports buffer before I can begin to process it.

The only way I can find is thread.sleep(), but it behaves differently on different computers. On one it downloads the data in 53 seconds, on another one it downloads in 11 minutes.
Anyone knows why and how to solve this problem?

This article has been dead for over six months. Start a new discussion instead.