J
JJ
I've done a little multi-threading on winform apps some time ago now, so I'm
not a complete beginner, but best assume I am for any explanations..:
This is an asp.net 2.0 website, using c#:
I have a thread whose job is to send several emails (i.e. a long task that
mustn't be run by more that one user at a time).
It all works fine, but I am concerned about the lifetime of a thread in the
case of smtp errors, and how it will work with several users trying to run
the same process.
The thread is started like:
ParameterizedThreadStart pts = new ParameterizedThreadStart(SendEmails);
Thread thread = new Thread(pts);
thread.Name = "SendEmails";
thread.Priority = ThreadPriority.BelowNormal;
thread.Start(parameters);
Several static variables are written to by the main calling code (the main
app) and then by the 'SendEmails' thread, in the following manner:
Lock.AcquireWriterLock(Timeout.Infinite);
Email.SentMails = 0;
Email.IsSending = true;
Lock.ReleaseWriterLock();
I am therefore assuming that if another user is viewing the pages and tries
to run the email sending process, I can check to see if the Email.IsSending
is set and if it is, don't allow the 'SendEmails' thread to be run and
present a 'busy' page. Does this sound correct?
My questions are:
1. If there are some unexpected errors in the SendEmails thread, would the
thread ever end? Should threads always be started with some sort of
timeout - of so how ? Should the thread's code always be in some sort of
try..catch ?
2. To do actions on the database only when the thread finishes, (i.e. using
Thread.Wait()), I assume I have to start a main calling thread first, which
itself will wait for the SendEmails thread to finish before it does its job?
3. How can you check if a particular thread is running, whichever user it
was started by, so I don't accidentally try to start another one? (is my
approach with setting a static flag for a particular thread - in this case
'IsSending' - the best approach?
4. If I was to have a 'Cancel Sending' button, is there any cleaning up or
waiting I need to do (apart from resetting Email.IsSending = false), or is
it just simply Thread.Abort?
Sorry for the numerous questions,
JJ
not a complete beginner, but best assume I am for any explanations..:
This is an asp.net 2.0 website, using c#:
I have a thread whose job is to send several emails (i.e. a long task that
mustn't be run by more that one user at a time).
It all works fine, but I am concerned about the lifetime of a thread in the
case of smtp errors, and how it will work with several users trying to run
the same process.
The thread is started like:
ParameterizedThreadStart pts = new ParameterizedThreadStart(SendEmails);
Thread thread = new Thread(pts);
thread.Name = "SendEmails";
thread.Priority = ThreadPriority.BelowNormal;
thread.Start(parameters);
Several static variables are written to by the main calling code (the main
app) and then by the 'SendEmails' thread, in the following manner:
Lock.AcquireWriterLock(Timeout.Infinite);
Email.SentMails = 0;
Email.IsSending = true;
Lock.ReleaseWriterLock();
I am therefore assuming that if another user is viewing the pages and tries
to run the email sending process, I can check to see if the Email.IsSending
is set and if it is, don't allow the 'SendEmails' thread to be run and
present a 'busy' page. Does this sound correct?
My questions are:
1. If there are some unexpected errors in the SendEmails thread, would the
thread ever end? Should threads always be started with some sort of
timeout - of so how ? Should the thread's code always be in some sort of
try..catch ?
2. To do actions on the database only when the thread finishes, (i.e. using
Thread.Wait()), I assume I have to start a main calling thread first, which
itself will wait for the SendEmails thread to finish before it does its job?
3. How can you check if a particular thread is running, whichever user it
was started by, so I don't accidentally try to start another one? (is my
approach with setting a static flag for a particular thread - in this case
'IsSending' - the best approach?
4. If I was to have a 'Cancel Sending' button, is there any cleaning up or
waiting I need to do (apart from resetting Email.IsSending = false), or is
it just simply Thread.Abort?
Sorry for the numerous questions,
JJ