473,324 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

How to gui from freezing when using while loop

2
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.Best regards.
Below are my lines of program:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. private void btnConnect_Click(object sender, EventArgs e)
  4.         {
  5.             if (ftStatus != FTDI.FT_STATUS.FT_OK)
  6.             {
  7.                 label1.Text = "Gerät Nicht Verbinden";
  8.             }
  9.             else
  10.             {
  11.                 label1.Text = " Gerät Verbunden";
  12.             }
  13.         }
  14.  
  15.         private void btndisconnect_Click(object sender, EventArgs e)
  16.         {
  17.             myFtdiDevice.Close();
  18.  
  19.             if (myFtdiDevice.IsOpen)
  20.             {
  21.                 label1.Text = "Gerät Verbunden";
  22.             }
  23.             else
  24.             {
  25.                 label1.Text = "Gerät Getrennt";
  26.             }
  27.         }
  28.  
  29.         private void LedOneButton_Click(object sender, EventArgs e)
  30.         {
  31.  
  32.  
  33.             UInt32 numBytesWritten = 0;
  34.             data[0] = 1;
  35.             myFtdiDevice.Write(data, 1, ref numBytesWritten);
  36.             data[0] = 1;
  37.             myFtdiDevice.Write(data, 1, ref numBytesWritten);
  38.             data[0] = 0x6A;
  39.             myFtdiDevice.Write(data, 1, ref numBytesWritten);
  40.         }
  41.  
  42.         private void rxtemp_CheckedChanged(object sender, EventArgs e)
  43.         {
  44.             if (this.rxtemp.Checked && !this.backgroundWorker1.IsBusy)
  45.             {
  46.                 this.backgroundWorker1.RunWorkerAsync();
  47.             }
  48.             else if (!this.rxtemp.Checked && this.backgroundWorker1.IsBusy)
  49.             {
  50.                 this.backgroundWorker1.CancelAsync();
  51.             }
  52.         }
  53.         private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  54.         {
  55.             UInt32 numBytesRead = 0;
  56.             UInt32 numBytesToRead = 1;
  57.             byte[] readData = new byte[10];
  58.  
  59.  
  60.             while (!this.backgroundWorker1.CancellationPending)
  61.             {
  62.                 // Do some work.
  63.                 Thread.Sleep(1000);
  64.                 ftStatus = myFtdiDevice.Read(readData, numBytesToRead, ref numBytesRead);
  65.                 // Update the UI.
  66.                 this.backgroundWorker1.ReportProgress(0, readData[0].ToString());
  67.  
  68.             }
  69.  
  70.         }
  71.  
  72.         private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
  73.         {
  74.             label3.Text = (string)e.UserState + "ºC";
  75.         }
  76.  
  77.  
  78.  
  79.  
Nov 3 '14 #1
1 14710
iam_clint
1,208 Expert 1GB
I don't see any reason your gui should freeze with this particular code block. I would need to see more of it.
When your gui is froze and you push pause in the debugger what line of code does it show its at?
Nov 4 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Egor Shipovalov | last post by:
I'm implementing paging through search results using cursors. Is there a better way to know total number of rows under a cursor than running a separate COUNT(*) query? I think PostgreSQL is bound...
4
by: CroDude | last post by:
I've made a custom groupbox control. Inside, as one of it's members is CSimpleGradient object. CSimpleGradient is a wrapper class for gradient usage. Basically it looks like that: public class...
2
by: Ash | last post by:
Hi, I'm working on an application that takes csv files then converts it to XML. It then write the XML to a msmq where another service is reading the queue and sends it to the DB. My question...
13
by: Bev in TX | last post by:
We are using Visual Studio .NET 2003. When using that compiler, the following example code goes into an endless loop in the "while" loop when the /Og optimization option is used: #include...
11
by: Alexander Bosch | last post by:
Hi, I'm having a problem similar to the one that's stated in this KB http://support.microsoft.com/default.aspx?scid=kb;en-us;839521 When I'm posting a page to itself with the bool value as true it...
0
by: jeremy | last post by:
Had a tough time figuring this one out and couldn't find a good solution, so I thought I would post this and hopefully it will help someone out. When using DataBind to dynamically bind a list to...
3
by: nico3334 | last post by:
I'm filling in a Report with SQL data using VB code. I'm using LOOP and MoveNext. Before using MoveNext, I would like to be able to check whether the new data is equal to the previous data that was...
5
by: boss1 | last post by:
hi all, i have a problem with loop in select statement.i m using code : <select name = "s" size = "1" > <option selected>P-Code</option> <option...
24
by: kindrain | last post by:
the code checks whether a.txt has exact the same lines, then write different lines into b.txt Here it is: (before that ,you should creat a.txt) ---------------------- #include<stdio.h>...
2
by: kako0000000 | last post by:
Hello I used textbox in my project i write a number and some thing writing i textbox using loop for example i used this code Private Sub Command1_Click() Dim i As Integer For i = 0 To 10...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.