Creating multi threads?

asokfair

Member
Joined
Jan 13, 2013
Messages
22
Programming Experience
Beginner
hi,

I am reading serial port values in VB.net , after reading i'm comparing the received values in case statement.
based on the received value i want start different process simultaneously. i know it could be possible with handling threads.
but i dont know how to create. this i given my normal code by calling process. Can anyone give the solution to convert into multiple threads?

Try
Select Case SerialPort1.ReadExisting()
Case "00"
start_process1()
Case "01"
start_process2()
Case "02"
start_process3()
Case "03"
start_process4()

End Select


thanks,
ashok r
 
What exactly do you mean by start different processes? Do you just mean calling a different method for each one or do you actually want to start an external application? What exactly do these "processes" do? Given that the DataReceived event of the SerialPort is already raised on a secondary thread, it may well be that you can simply do your work on that thread, as long as it doesn't take too long.
 
I want to play the music files simultaneously based on the serial data.I am using 4 different media players inside my app
 
I'm not sure that that would be possible anyway but there's an easy way to test it. Like I said, the SerialPort raises its DataReceived event on a secondary thread anyway, so you can simply go ahead and do what you want to do. If it successfully plays multiple sounds simultaneously then you know it works and you can then look to optimise it; otherwise, don't waste your time.
 
your correct.

but let take if i received the value of "01" and immediately the DataReceived event will start. meanwhile executing the DataReceived Event if i received another value of "02" what will happen?
 
your correct.

but let take if i received the value of "01" and immediately the DataReceived event will start. meanwhile executing the DataReceived Event if i received another value of "02" what will happen?

DataReceived is raised on a ThreadPool thread. When the event is raised, the next available thread is taken from the pool and used. In your example, if the first thread is still busy working when the second event is raised then the second event will be raised on a different thread. If a third event is raised while the previous two tasks are still running then a third thread will be used. This can't go on indefinitely but, if the number of tasks is relatively small and they don't take too long to complete then it is safe to just use those threads for everything.
 
Thank you,

But in feature i may add more case statements inside the DataReceived Event. It should not affect my call routines.
Problem is In minimum interval if i receives data, is there the chances to miss. So i thought to split the DataReceived Event into 2 or 4 threads inside of threads i will write my case statement. its possible to implement in this way?

thanks,

ashok r
 
Back
Top