1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Gui stopped responding after two updates

Discussion in 'C#' started by firstoption, Nov 12, 2014.

  1. firstoption

    firstoption New Member

    Joined:
    Apr 1, 2014
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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.
    Code:
    
    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

    Code:
    
    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
    Code:
    
    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.
     

Share This Page