Trapping a button click in a For or while

gnappi

Newcomer
Joined
Mar 7, 2006
Messages
13
I have a program that samples the serial port continually based on a number the user provides. Ive been trying to get an event to STOP the loop with a button without success.

For example:

While stop_button = false

for user_count = 1 to variable_number

serialportx.readline

' here I want to trap an event that sets user count to 20 or stop the for loop
' from executing.

next

end while

Any ideas?

I never can get the following when the loop is executing.

1. A button click event to return a TRUE
2. a status of the button (TRUE or FALSE) to print to a textbox.
3. Any user control to be visible or not visible

What is it about the loop that I'm missing?

Any help would be appreciated.

Thanks,

Gary
 
It looks like your polling process is running in the main thread. Since it is a synchronous process your main form is not going to respond to any GUI events while the polling process is running. What you need to do is run the polling process in a second thread. This will allow you to maintain a responsive GUI while the polling process is running in the background.

Here's how I would do it in C#:
C#:
class PollingExample
{
    readonly object _lock = new object();

    bool _poll = false;

    private bool ShouldPoll
    {
        get { lock (_lock) { return _poll; } }
        set { lock (_lock) { _poll = value; } }
    }

    private void Poll()
    {            
        while (ShouldPoll)
        {                
            // do polling...
        }
    }

    public void Start()
    {
        if (!ShouldPoll)
        {
            ShouldPoll = true;

            Thread thread = new Thread(Poll);
            thread.IsBackground = true;
            thread.Start();
        }
    }

    public void Stop()
    {
        if (ShouldPoll)
        {
            ShouldPoll = false;
        }
    }
}
You could then create an instance of this class and call the Start/Stop methods from your form.

An important note:

Although a bool's value can be read/written in an atomic operation it is still a good idea to wrap it in a lock to make sure that its value is fresh. This page has some good information on this. You can also simply flag the variable with the volatile keyword but I prefer to use the lock.
 
I would look into threading. This is what I use when I make a test program.

here is a quick example
Visual Basic:
Public Class frmHBmain
    Inherits System.Windows.Forms.Form
    dim thrdTest as thread

Private Sub btnStartTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartTest.Click
        thrdTest = New Thread(AddressOf StartTest)
        thrdTest.Start()
End Sub


 Private Sub btnStopTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopTest.Click
        If thrdTest.IsAlive Then thrdTest.Abort()
 End Sub

Private Sub StartTest
   'Enter your while loop or any other code.
End Sub

End Class
 
Back
Top