# Visual Basic > Visual Basic 6 and Earlier >  Program flow during an _OnComm Event (MSCOMM32.OCX)

## daveyk021

Interesting thing I found out.  Program flow continues in the sub program that sent code to the serial port, even though, at the exact same time, code is running in the _onComm event sub program.

I wrote a work around for the main program to pause while the _oncomm event is running, but isn't this multi-threading kind of with VB6?  I also no you can get the same program to start two tasks at the same time (oh it's a mess, but you can do that).

The main issue I had was the program was 20-30 lines down the code while the input buffer was still be read in the _oncomm sub program.  This was not good.  This seems to have solves that:
'
MSComm1.Output = Remote & vbCr
'
TimeOutZeit=2
start = Timer
Do                                      
            DoEvents

            If Timer > start + TimeOutZeit Then
                timeout = True
                Exit Do
            End If

            Loops = Loops + 1

Loop Until InStr(strBuffAll, vbCr) <> 0 Or InStr(strBuffAll, "*E4 *")

Basically that causes the program to halt while the _oncomm event is running.  I assume there is no other settings that do that?

----------


## DataMiser

hmm, My guess would be that you have some do events in the on comm routine causing your issue.

----------


## wqweto

Fighting fire with fire? You added more DoEvents in your code when it's teh proverbial lurking DoEvents which is causing the unwanted "multi-threaded" behavior is first place.

Everyone should have already seen numerous warnings and horror stories in these forums about how one should not slap DoEvents around practically *never* when one does not know what they are doing.

cheers,
</wqw>

----------


## daveyk021

Thank you, the problem is that without them it's easy to not to be able to <Ctrl><c> the program and have to shut it down with the task manager.  Is there an alternative?

----------


## DataMiser

> Thank you, the problem is that without them it's easy to not to be able to <Ctrl><c> the program and have to shut it down with the task manager.  Is there an alternative?


You should not need to, If you are having some long running loop in your on comm event that makes you need to kill the program at times then your code needs to be re-done. You should nto be looping in that event for more data rather you get whatever is there, add it to a buffer and move on. If the data is complete then you process it but never set in a loop waiting on it and definitely don't use do events in there you are just asking for problems when you do.

----------

