Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

how to prevent gui from freezing when using a while loop

c#

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

#1 firstoption

firstoption

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 03 November 2014 - 08:13 AM

Good day to all,
Please I need your guide and support on how i can stop my GUI from freezing.I have 3 buttons,
and a checkbox on my GUI.Two of the buttons are used to connect and disconnect my application to the
serial port while the third button is used to turn ON/OFF LED.The checkbox is used to receive DATA
from the microcontroller whenever it is checked.
I have two functions in my microcontroller.One function sends DATA continuosely to the GUI and get updated)
while the second function receives DATA from the GUI to ON/OFF the LED.The problem i am having is that whenever i run the
two function concurrently(that is sending data to GUI and receiving data from the GUI at the same time)
my GUI freezes.However if i run any of the function(that is i disable one of the functions)then the GUI will not
not freez,everything will run as expected.After some research on the internet,i got the idea of backgroundworker and threading.I have tried
to used these two options separately but my GUI still freezes.I am still new to C#.You suggestions and ideas will be highly appreciated.
Below are my lines of program:

private void btnConnect_Click(object sender, EventArgs e)
        {
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                label1.Text = "Gerät Nicht Verbinden";
            }
            else
            {
                label1.Text = " Gerät Verbunden";
            }
        }
 
        private void btndisconnect_Click(object sender, EventArgs e)
        {
            myFtdiDevice.Close();
 
            if (myFtdiDevice.IsOpen)
            {
                label1.Text = "Gerät Verbunden";
            }
            else
            {
                label1.Text = "Gerät Getrennt";
            }
        }
 
        private void LedOneButton_Click(object sender, EventArgs e)
        {
 

            UInt32 numBytesWritten = 0;
            data[0] = 1;
            myFtdiDevice.Write(data, 1, ref numBytesWritten);
            data[0] = 1;
            myFtdiDevice.Write(data, 1, ref numBytesWritten);
            data[0] = 0x6A;
            myFtdiDevice.Write(data, 1, ref numBytesWritten);
        }
 
        private void rxtemp_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rxtemp.Checked && !this.backgroundWorker1.IsBusy)
            {
                this.backgroundWorker1.RunWorkerAsync();
            }
            else if (!this.rxtemp.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)
        {
            label3.Text = (string)e.UserState + "ºC";
        }
           



#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 03 November 2014 - 07:24 PM

Welcome aboard.

 

Check for Apllication.DoEvents()  and add it to the loop, ;)  it will help your problem.






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