|
-
Sep 30th, 2011, 09:42 PM
#1
Thread Starter
Fanatic Member
how to determine each thread is already finished to run
Hello All,
I have 2 threads that willl runnning in a timer_tick event
how to determine each thread is already finished to run
I want to try IsAlive .. but I want to make sure.. does IsAlive property will return false when the thread completely finished ?
or any other idea or suggestion?
Thanks
Code:
Thread t1;
Thread t2;
..
..
..
private void runningTimer_Tick(object sender, EventArgs e)
{
_second++;
if (_second >= SECOND_HOUR)
{
if (!t1.IsAlive && !t2.IsAlive)
{
_second = 0;
t1.Start();
}
}
else
{
if (!t2.IsAlive && !t1.IsAlive)
{
t2.Start();
}
}
}
-
Oct 3rd, 2011, 09:25 PM
#2
Re: how to determine each thread is already finished to run
This looks like an incorrect use of threading - what are you trying to achieve that you think you need threading for?
-
Oct 3rd, 2011, 09:32 PM
#3
Thread Starter
Fanatic Member
Re: how to determine each thread is already finished to run
I want to send SMS to some recipients, t1 is to send birthday SMS and t2 is send SMS notification ..
since the SMS is a serial device so only one thread can access the device at the same time.
any suggestion?
-
Oct 3rd, 2011, 09:51 PM
#4
Re: how to determine each thread is already finished to run
Don't use multi-threading, and call it from the main UI thread. I can't believe it's going to take that long to send the SMS is it?
-
Oct 3rd, 2011, 09:54 PM
#5
Re: how to determine each thread is already finished to run
If you're worried about accidentally calling it from multiple threads even then, you can use the synchronisation primitives inside your SMS abstraction (you have encapsulated the SMS service behind some kind of interface*, right?) to prevent execution of the critical block of code simultaneously.
* Not as in the keyword interface, but the layman's meaning - could equally be a class, a method, or some other kind of abstraction.
-
Oct 5th, 2011, 11:41 AM
#6
PowerPoster
Re: how to determine each thread is already finished to run
best practice here is to NOT do threading because the task is not a time consuming task. Threading is expensive and should only be used in time consuming tasks, rightly so said by evil giraffe.
the IsAlive property does however tell you if the thread has completed but you can have a problem here. what if the Thread was aborted or stopped in a non normal way? (i.e an error occured) - you would still get true for the property value.
the best thing here would be to use Auto or ManualResetEvents and also WaitHandle.WaitAll a collection of them and wait until all the signals have been indicated/set that the thread has finished doing the task at hand (based upon your business logic rules)
http://msdn.microsoft.com/en-us/libr...esetevent.aspx
http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx
but again, we are over doing work because for a small task you are describing, does not require to be threaded.
-
Oct 6th, 2011, 06:06 AM
#7
Re: how to determine each thread is already finished to run
Would the ThreadPool suit your needs?
You could set the max workers to 1 and queue all the messages.
Alternately you could just let the thread process a queue (as in collection).
Last edited by TheBigB; Oct 6th, 2011 at 06:10 AM.
Delete it. They just clutter threads anyway.
-
Oct 6th, 2011, 06:12 AM
#8
PowerPoster
Re: how to determine each thread is already finished to run
again.... for a task that is NOT time consuming - why thread?
-
Oct 6th, 2011, 06:49 AM
#9
Re: how to determine each thread is already finished to run
Well if I understand correctly the messages are being sent through a phone connected as a serial device.
This can take a second or two for each message depending on signal strength etc.
And there could be multiple messages to be sent.
So in terms of keeping the UI thread responsive it's not a bad idea.
Delete it. They just clutter threads anyway.
-
Oct 6th, 2011, 06:50 AM
#10
PowerPoster
Re: how to determine each thread is already finished to run
actually, it still is. a second or two is NOT a long running task AT ALL. in addition, keeping the UI responsive does not necessarily mean spin up another or multiple threads (depends on the context though!)
what you want to do, is batch them up, then send them on a background thread. not a thread per message.
the OP's code snippet for a timer and monitoring 2 threads is also... not entirely efficient and would probably cause more problems. best thing is to batch up the messages into a list, then on the timer, check to see if there are messages to be sent and if so - spin up the thread and send the messages from that thread but making sure that you are using thread safe techniques if you are "inspecting" the list from outside the thread.
-
Oct 6th, 2011, 06:54 AM
#11
Re: how to determine each thread is already finished to run
Alright, I can agree on that.
Delete it. They just clutter threads anyway.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|