本文介绍了如何使用backgroundworker / threading来防止我的GUI冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,

我需要你的指导和支持我如何阻止我的GUI冻结。我有3个按钮,

和一个复选框我的GUI.Two按钮用于连接和断开我的应用程序到

串口,而第三个按钮用于打开/关闭LED。复选框用于接收DATA
每当检查时,微控制器都会产生


我的微控制器中有两个功能。一个功能将DATA连续发送到GUI并得到更新)

while第二个函数从GUI接收DATA以打开/关闭LED。我遇到的问题是每当我同时运行

两个函数时(即将数据发送到GUI并从GUI接收数据)同时)

我的GUI冻结。但是如果我运行任何功能(即我禁用其中一个功能)那么GUI将不会

not freez一切都会按预期运行。经过一些互联网研究,我得到了身份证我已经尝试过

分别使用这两个选项但我的GUI仍然冻结。我还是C#的新手。你的建议和想法将受到高度赞赏。

以下是我的程序:





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";
     }

推荐答案

UInt32 numBytesWritten = 0;
data[0] = 1;
myFtdiDevice.Write(data, 1, ref numBytesWritten);

如果myFtdiDevice是一个serialPort,那么这是一些奇怪的代码,因为SerialPort.Write不接受 ref 参数,而第二个参数是一个偏差,大概应该是零以匹配您输入数据的位置。



我怀疑这是你遇到问题的地方 - 但你需要看看你到底是什么试图在这里做。我这样做:

If myFtdiDevice is a serialPort, then that is some odd code, since SerialPort.Write doesn't take a ref parameter, and the second parameter is an offset which presumably should be zero to match the location you are putting the data in.

I suspect that this is where you re getting the problem - but you need to look at exactly what you are trying to do here. I'd do it like this:

byte[] data = new byte[] { 1, 1, 0x6A};
myFtdiDevice.Write(data, 0, data.Length);





但是如果没有你的硬件检查,很难说!



But without your hardware to check against, it's very difficult to tell!


这篇关于如何使用backgroundworker / threading来防止我的GUI冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:17
查看更多