Gui stopped responding after two updates

firstoption

Member
Joined
Apr 23, 2014
Messages
10
Programming Experience
Beginner
Good day to all,
Please i need your help and support on how to resolve this problem.My UI thread stopped responding after two updates.In an attempt to solve this problem,i had tried to use Backgroundorker and Asynchronouse programming.With the backgroundwork,my GUI did not respond.Values sent to the GUI did not even show at all,so there was nothing like
updates when i used backgroundworker.However when i used Asynchronouse programming,the value sent to the GUI was displayed and it was updated twice(for example,if the value sent to the GUI is 10,this value(10)will be displayed and then updated to 12).The values sent to the GUI comes from the return value of the function below:

C#:
ftStatus = myFtdiDevice.Read(readData, numBytesToRead, ref numBytesRead);
           value = Convert.ToString(readData[0]);
           return value;


This function reads value from the SerialPort at intervals which is determine by the Timer.I have on my form 4 buttons,one richtextbox with a label and one checkbox.The first two buttons connects my GUI to the SerialPort while the remaining two buttons are used to ON/OFF Leds.The Checkbox is used to receive data from the SerialPort whenever it is checked.One more observation with the Asynchronouse code is that,i can still move the GUI form around whenever it stops updating, however,if i click any of the four buttons afterward,then the whole of the form freezes.
Below are my program codes for the Backgroundworker and Asynchronouse respectively.

BACKGROUNDWORKER CODE


C#:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked && !this.backgroundWorker1.IsBusy)
            {
                this.backgroundWorker1.RunWorkerAsync();
            }
            else if (!this.checkBox1.Checked && this.backgroundWorker1.IsBusy)
            {
                this.backgroundWorker1.CancelAsync();
            }
        }
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            UInt32 numBytesRead = 0;
            UInt32 numBytesToRead = 1;
            byte[] readData = new byte[10];


            while (!this.backgroundWorker1.CancellationPending)
            {
                // Do some work.
                Thread.Sleep(1000);                ftStatus = myFtdiDevice.Read(readData, numBytesToRead, ref numBytesRead);
                // Update the UI.
                this.backgroundWorker1.ReportProgress(0, readData[0].ToString());

            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            label11.Text = (string)e.UserState + "?C";

        }


ASYNCHRONOUSE CODE




C#:
private  void rxtemp_CheckedChanged(object sender, EventArgs e)
       {

           aTimer = new System.Timers.Timer(1000);
           aTimer.Elapsed += new ElapsedEventHandler(DisplayTempValue);
           aTimer.AutoReset = true;
           aTimer.Enabled = true;
      
       }

       private string HeavyOperation()
       {
           UInt32 numBytesRead = 0;
           UInt32 numBytesToRead = 1;
           byte[] readData = new byte[255];
           string value;
           ftStatus = myFtdiDevice.Read(readData, numBytesToRead, ref numBytesRead);
           value = Convert.ToString(readData[0]);
           return value;
       }

       private Task<string> HeavyOperationAsync()
       {
           return Task.Run<string>(() => HeavyOperation());

       }
       private async void DisplayTempValue(object source, ElapsedEventArgs e)
       {
           string result = await HeavyOperationAsync();
           label3.Invoke(new Action(() =>
           {
               label3.Text = result + "?C";

           }));
       }

I would be very glad if somebody could put me through on how to resolve this Problem. i have no idea of what is going on behind the Scene.Your advice and Suggestion will be highly appreciated.Thank you for the usual Support.Best regards.
 
Back
Top Bottom