Spawning multiple processes at same time

  • Thread starter Thread starter Jeremy
  • Start date Start date
J

Jeremy

I have a core VB service that monitors a database, and based on data in the
records will execute code to send email notifications.

Problem:
I don't want my main program code to halt and wait for this execution... we
need immediate execution of all notification processes and would prefer to
spawn multiple instances generating emails at one time.

Solution ?
I took a look at threading, and at first it looked to be the answer.
Unfortunately, I need to spawn an unlimited amount of processes (maybe
manage the number based on resources) and threads have to be Dim'd and
executed one at a time. Not what I am looking for.

So, any ideas?

Jeremy
 
I think a bit more information on the problem you're trying to solve may
help you get solution sugesstions.

Your "problem" with threads sounds a bit like you may not have a handle on
them: They can execute "at the same time", not one at a time. Yes, they
have to be created and started at some point, but they will be lighter
weight than the same number of processes.
 
That is true. I have used them in the past to complete concurrent processes
and then return with the results to the controlling UI. In this case, I
need to create an unknown number of concurrent processes to create email
notifications.

More information:
I am basically writing my own scheduler program. It queries our database to
pull down the execution pattern for all applications that use its email
notification process. I allow the application developers to set the
interval or schedule via database entries. In some cases it would be an
interval, such as every 10 minutes, while others are more of a set schedule,
like every Tuesday at 11pm.

I pull down a complete list of the applications, there schedule and the last
time they ran. I then calculate if they need to run. This is where I would
like to just spawn a process to handle the email text generation, which
would be based on the specific application, and then continue working
through the application list to find any others that need to be run.

I plan to insert a database flag so I know I ran a given application
already, but the core process doesn't care how the spawned process ends. If
errors occur, I will handle logging them in the spawned process. The core
process is to just keep on moving.

It comes down to the potential of the spawning process... I don't have a set
number that could be executed, therefore I don't believe I can use threads
since I can't dynamically name a variable and create it as a thread for
execution.

Comments? Solution?
 
I can give you just a few observations. Sounds like a fun little project,
though!

1) You probably don't want to fire off a thread for each email to be sent -
if there are lots of threads / processes running a computer will spend all
its time switching contexts and not enough time running your code. (besides
the whole resource starvation issue)

2) You can use System.Threading.ThreadPool.QueueUserWorkItem to have a
delegate called on a separate thread. All the plumbing of making sure the
threads are scheduled correctly is already done for you. Kind of a "Here do
this when you can" facility. Note that this has some issues with possible
thread starvation, and ASP.NET uses these internally as well, so go into it
with eyes open. However, this does seem like a good possibility for your
problem.

3) Async delegates may be a possibility for you.

4) You could use an ArrayList of Thread objects to start and control them.

Concept example for #2

Public Class StateHolder
'This class holds state information to be passed from thread to thread
Public id as Integer
End Class
....

Public Sub MyMainThreadProcessingLoop()
...
'This is where you fire off your threads
Dim state as new StateHolder
state.id = someIDValueFromTheLoop
System.Threading.ThreadPool.QueueUserWorkItem(AddressOf(DoSomething),
state)
...
End Sub

Public Sub DoSomething(State as Object)
Dim s as StateHolder = CType(State, StateHolder)
'Now I have my information, do something. This Sub will be called
on a separate thread!
...
End Sub
 
Is it possible to execute the same code block (sub) as the process for more
than one thread? I have attempted this with no success.

Jeremy Wood
 
I've done this many times with no problems.. What kind of issues are you
having?

(there's a quick example of threading with waithandles up on my blog at
http://philiprieck.com/blog/archive/2004/01/27/WaitHandleSample.aspx
-- while it's not exactly what we're talking about it does use multiple
threads using the same worker class to provide functionality. let me know
if it helps / doesn't help. Worst case, I can try to find some time to put
up a sample on QueueUserWorkItem, although you may need to wait a bit for
that.)
 
Back
Top