Click here to Skip to main content
14,691,021 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a bit of a conundrum, I have a piece of code and would like to wait until certain criteria is met, without blocking, before moving on to the next line of code.

Here is the flow :

-Send packet over serial comms
-Start Timeout Timer Event
-Wait until receive flag is set <=not sure what to do here .Receive Serial Event
-If receive flag set move on
-Send New packet over Serial comms .... starts process over again.

In the timeout timer event, will basically resend the data. My question is, how do I wait until the specific flag is set, BEFORE moving to the next line of code.
Application.Doevents from what I have read is a very bad way of performing this.
Posted
Comments
wizardzz 16-Jan-12 3:55am
   
Have you considered using event based code for this?

1 solution

Application.DoEvents is a bad way to do it, yes.

Events are you way to go. They do not have to be just the existing control events, your form (or any other class) can create, handle and signal its own events. Handle the SerialPort.DataReceived and when the right data has been received, transfer to your other code by signalling an event of your own. Your event handler than continues and prepares for the next bit.
   
Comments
codenameyash 16-Jan-12 4:21am
   
Agreed, I quite like the events idea, Yes I do have a serial Data Received handler, however, say I do create something like a Delegate and subscribe to that delegate which notifies me when the correct flag has been set, How does that stop my other code from being executed in the mean time?
OriginalGriff 16-Jan-12 4:39am
   
You could do it with delegates, but I wouldn't: literally use events: "I have been polled" "I have data from the device: here it is" and so forth. It's difficult to explain: you seem to be locked into a linear way of processing the data, which is very difficult to "blend in" with the event driven, non linear Windows model.
The other alternative is to set up a background task with does process data in a linear fashion, and which waits for changes to be indicated by the event handlers in your main thread.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900