By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,641 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,641 IT Pros & Developers. It's quick & easy.

Spawning multiple processes at same time

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
"Jeremy" <ww*****@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #2

P: n/a
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?

"Philip Rieck" <st***@mckraken.com> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl...
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.
"Jeremy" <ww*****@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #3

P: n/a
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(Addr essOf(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

"Jeremy" <ww*****@comcast.net> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
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?

"Philip Rieck" <st***@mckraken.com> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl...
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.
"Jeremy" <ww*****@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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



Nov 20 '05 #4

P: n/a
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

"Philip Rieck" <st***@mckraken.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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(Addr essOf(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

"Jeremy" <ww*****@comcast.net> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
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?

"Philip Rieck" <st***@mckraken.com> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl...
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.
"Jeremy" <ww*****@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> 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
>
>



Nov 20 '05 #5

P: n/a
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/...dleSample.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.)


"Jeremy" <ww*****@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

"Philip Rieck" <st***@mckraken.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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(Addr essOf(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

"Jeremy" <ww*****@comcast.net> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
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?

"Philip Rieck" <st***@mckraken.com> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl...
> 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.
>
>
> "Jeremy" <ww*****@comcast.net> wrote in message
> news:%2****************@TK2MSFTNGP10.phx.gbl...
> > 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
> >
> >
>
>



Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.