Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

GUI STOPPED RESPONDING AFTER TWO UPDATES

c#

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 firstoption

firstoption

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 12 November 2014 - 03:55 AM

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.

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

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

 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.

 



#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 12 November 2014 - 04:56 AM

Thing is, the form is a "thread" whose workings stop because yours is taking all the resources.

To change the situation use Application.DoEvents() inside your task's loops,

DoEvents include the mouse move, keypress, etc.



#3 firstoption

firstoption

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 12 November 2014 - 05:18 AM

thank you very much sir for  taking the time to go through my code.I really appreciate your sense of understanding.Sir,could be please  give me an idea  where to place the Application.DoEvents() in my  methods.I have already applied it but i got the same Response.Your Support will be highly appreciated in this regards.Once again thank you for the guide.

Best regards.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download