Creating a loop around a checkedbox

firstoption

Member
Joined
Apr 23, 2014
Messages
10
Programming Experience
Beginner
Good day to all,
Please i need your support on how to modify the program below so that it can read the data(temperature value) sent from the microcontroller as long as the Checkbox is checked.At the moment the function
C#:
myFtdiDevice.Read(readData, numBytesToRead, ref numBytesRead);
reads only the first temperature value sent from the microcontroller,the subsequent temperatue values are not being updated even though the Checkbox is still checked.I would be very glad if somebody could put me through on how to create a Loop as Long as the Checkbox is checked.Thank you for the usual Support.Best regards.




C#:
 private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            UInt32 numBytesRead = 0;
            UInt32 numBytesToRead = 1;
            byte[] readData = new byte[10];
           ftStatus = myFtdiDevice.Read(readData, numBytesToRead, ref numBytesRead);
        
           label11.Text = Convert.ToString(readData[0]);
     
        }
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,151
Location
Sydney, Australia
Programming Experience
10+
You won't be putting the code in that CheckedChanged event handler for a start. If did then your UI would freeze up and you'd never be able to uncheck the box. I'd probably recommend that you employ a BackgroundWorker for this. Here's a simple example of what you might do to start and stop a background task using a CheckBox and to update the UI while that background task is occurring:
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)
{
    while (!this.backgroundWorker1.CancellationPending)
    {
        // Do some work.
        Thread.Sleep(123);

        // Update the UI.
        this.backgroundWorker1.ReportProgress(0, DateTime.Now.ToString());
    }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    this.label1.Text = (string) e.UserState;
}
 

firstoption

Member
Joined
Apr 23, 2014
Messages
10
Programming Experience
Beginner
Thank you for taking the time to respond to my question.i really appreciate your Kind gesture.I will try and include the Backgroundworker in my program as you suggested,although i have not used it before in my previouse programs as i have just started learning C#.I will update you once i resolve the Problem.Once again thank you for the Support.Best regards.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,151
Location
Sydney, Australia
Programming Experience
10+
If you're not familiar with the BackgroundWorker then please don't just copy y code and expect it to work. Read the documentation for the class at least but I recommend some additional research besides. Perhaps you were going to do that anyway but I wanted to say it explicitly because plenty of people don't.
 

firstoption

Member
Joined
Apr 23, 2014
Messages
10
Programming Experience
Beginner
Good day Sir,
Thank you for the support .I really appreciate your kind gesture. After much struggle I got the backgroundworker working. I was able to update my GUI without any problem. However
I experience new development. My GUI freezes based on the fact that I am running two functions in my microcontroller main function. If I run one function(for example the function that send Data to the GUI)everything will work well,the GUI will be up dated correctly and it will not freez. I have actually opened a new tread for that problem. Once again thank you for the guide and support. Best regards.
 
Top Bottom