how to manage multiple threads

R

Raoul Minder

Hi all

I am new to threading!

I am developping a newsletter tool including a dispatch manager that
should schedule sendings. The Web layer kicks a remote object hosted by
a windows service and should 'add' a schedule (time to send, id and so
on). The result should be that I have multiple threads waiting for the
send time (with thread.sleep). Now the question: How can I abort or
change the 'time to send' if the web administrator changes the (already
scheduled) send time? In other words: How can I refer one of my already
thrown threads. Please refer to the threading question only because the
surrounding behaviour (like architecture and so on) is already given.

Thank you so much for an idea how to refer thrown threads.

For a better understanding of my question a code snipplet that - did I
mention it? - is a first try with threading:

---------------------------------
public void Schedule(string ConnectionString, int Id, DateTime
ScheduleDate)
{
_now = DateTime.Now;
_connectionString = ConnectionString;
_intSleep =
Convert.ToInt32(((TimeSpan)ScheduleDate.Subtract(_now)).TotalMilliseconds);

_id = Id;

Thread t = new Thread(new ThreadStart (RunThread));
t.Start();
}

private void RunThread()
{
if (_intSleep > 0) {Thread.Sleep(_intSleep);}
//... Send the threaded Newsletter somehow ...
}

public void ClearSchedule(string ConnectionString, int Id, DateTime
ScheduleDate)
{
//That's exactly my question (???)
}
---------------------------------
 
S

Scott Allen

Hi Raoul:

I think you should reconsider the threading approach you are using. If
someone wants a newsletter to go out 2 days from now - do you expect
the thread to sleep for two days? What if the server is reset during
that time? There are some drawbacks to this approach.

Consider this alternative: Keep the scheduled times to send a
newsletter in a collection. The more durable collection, the better.
You could use an ArrayList, but even better would be an XML file
(because it would survive server restarts), and even better still a
database table (because it takes care of atomic operations for you).

You could then use a thread and a System.Threading.Timer class to
periodically wake up a thread with an event (say every 10 seconds,
every 60 seconds, every 10 minutes, depending on how granular and
precise the send needs to be) which can check the collection and send
out newsletters (perhaps on a second thread).

You will use fewer threads, and have a more robust approach. In
addition, changing an existing schedule doesn't involve signaling or
interrupting a waiting thread, it's just changing a record in the
database.

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top