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.Best regards.
Below are my lines of program:
Expand|Select|Wrap|Line Numbers
- 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";
- }