Forms and interrupts

fmfm20j

Member
Joined
Dec 31, 2006
Messages
5
Programming Experience
10+
Hi,

I'm new to this forum but have been programming in languages other than VB.net for many years. Most of my programming has been very applications specific (electronic test) and it seldom requires interaction with graphical environments. Thus, I'm happy with programming as a concept but am less familiar with object oriented code tailored for user interaction.

OK, as a hobby, I'm in the process of developing some software to monitor a slot car track. The data comes in to my PC through a serial port and it contains information about the cars, timestamps as they cross the start/finish line, modes of racing etc. I have written code that can decipher this information and display the results on a form.
The bit that I am struggling with is the button control on the form and how this interacts with the program that reads the serial port and displays the results. I'll try to explain a little bit more:

I have a window for showing what is happening when the VB.net software runs (lap times, position etc) I can hit a start button on this form and this button (as a button click event) calls a subroutine which monitors the data coming into the serial port of the PC, processes it and displays the data on the windows form. This subroutine loops around for as long as the race runs. What stops it from continuing to run (naturally at least) is a sequence of codes from the serial port. Once these specific codes run, the subroutine decides that the race has stopped and it returns control back to the user form. This bit works OK (in isolation).

Sometimes, it is necessary to abort the race once the race has started. To do this, I have a separate STOP button and what I want to happen is for an event (button click) to stop the sub routine and return control back to the window. I found that the stop event simply wasn't reliably stopping the subroutine (I had doevents, etc in the code).

I then wondered if it was better to try to control the subroutine with threads. I set up a thread within the vb code behind the application window and started the thread running with the pressing of the START button. It seemed to be starting the subroutine OK. I put a thread abort behind the STOP user button but found that thread wasn't really stopping reliably. When I wanted to start another race (with the form still running) the application complained that the thread was still running and gave me run time errors. I didn't but any handshaking within the thread software (as I didn't really understand what I was trying to do!).

Being a little long in the tooth I do struggle with some of the more recent code terminology. Consequently, if you are kind enough to offer me some help, please keep the terminology simple so that I can follow it! I'd like to get your advice as to if my approach makes sense or if there is a better way to do this. If it does make sense, what are the likely traps that I am falling into. If there is a better way, please could you explain it to me. Suitable and simple to follow web topics may also help me to understand if you can reccommend some.

Many thanks

Ian
 
What I would do in this situation is set a variable in the form declarations, called "RaceAborted" as a boolean.

Each time your code loops, have it check if race aborted is true, and if so, end the loop.

Then all your stop button needs to do is set RaceAborted to true.

(just remember to have the Start button set it to false otherwise it won't work second time around!)
 
Thanks for replying to my mail. Your suggestion is actually what I finally did. I also discovered that I wasn't giving the operating system enough opportunities to interrogate the events software and, as a consequence, there was far too much latency addressing the events. I have everything related to that working now.
 
i would actually do all that in a separate thread, that way all your start button would need to do is start the thread and all your stop button would need to do is stop the thread. the thread can send data to the main thread (the form) through events
 
Back
Top