471,310 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Communicating between a main thread and a worker thread?

I have an object that spawns a worker thread to process one of its methods.
That method processes methods on a collection of other objects. During this
processing, a user may request to cancel the entire operation. I could
request abort on the worker thread, but that is a) potentially messy, and b)
not guaranteed to take immediate effect anyway. I would rather have some way
of allowing the main thread to tell the worker thread that it should stop.
The code being executed by the worker can then check periodically for a stop
request. An added complication is that this should be compatible with
ASP.NET, meaning that any use of static variables needs special
consideration.

I'd be grateful for any advice.
Jan 6 '06 #1
6 5752
Joe,

First off, if you are using ASP.NET, then you really have no reason to
spawn another thread. Your page is not going to be visible while processing
anyways, so you have no need for the extra thread.

If you are using this in a richer client app, then what you want to do
is have a field in the class the method is on which indicates whether or not
it should stop processing. You need to protect access to this field with a
lock statement, so that it is not corrupted if one thread tries to read and
one thread tries to write to it.

Once you have that, check through each iteration of the loop, and if the
flag is true, then stop processing the look, and exit the method graciously.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe Jax" <jj@nowhere.com> wrote in message
news:u6*************@TK2MSFTNGP10.phx.gbl...
I have an object that spawns a worker thread to process one of its methods.
That method processes methods on a collection of other objects. During this
processing, a user may request to cancel the entire operation. I could
request abort on the worker thread, but that is a) potentially messy, and
b) not guaranteed to take immediate effect anyway. I would rather have some
way of allowing the main thread to tell the worker thread that it should
stop. The code being executed by the worker can then check periodically for
a stop request. An added complication is that this should be compatible
with ASP.NET, meaning that any use of static variables needs special
consideration.

I'd be grateful for any advice.

Jan 6 '06 #2
Beside what Nicholas suggested I'd recomend reading through the following
MSDN article:
http://msdn2.microsoft.com/en-us/library/ts553s52.aspx

There are two solutions one is for .NET 2.0 only and the other is general
and works on all versions.
--

Stoitcho Goutsev (100)

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
Joe,

First off, if you are using ASP.NET, then you really have no reason to
spawn another thread. Your page is not going to be visible while
processing anyways, so you have no need for the extra thread.

If you are using this in a richer client app, then what you want to do
is have a field in the class the method is on which indicates whether or
not it should stop processing. You need to protect access to this field
with a lock statement, so that it is not corrupted if one thread tries to
read and one thread tries to write to it.

Once you have that, check through each iteration of the loop, and if
the flag is true, then stop processing the look, and exit the method
graciously.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe Jax" <jj@nowhere.com> wrote in message
news:u6*************@TK2MSFTNGP10.phx.gbl...
I have an object that spawns a worker thread to process one of its
methods. That method processes methods on a collection of other objects.
During this processing, a user may request to cancel the entire operation.
I could request abort on the worker thread, but that is a) potentially
messy, and b) not guaranteed to take immediate effect anyway. I would
rather have some way of allowing the main thread to tell the worker thread
that it should stop. The code being executed by the worker can then check
periodically for a stop request. An added complication is that this should
be compatible with ASP.NET, meaning that any use of static variables needs
special consideration.

I'd be grateful for any advice.


Jan 6 '06 #3
Thanks, I'll have a look at that.

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:O3*************@TK2MSFTNGP09.phx.gbl...
Beside what Nicholas suggested I'd recomend reading through the following
MSDN article:
http://msdn2.microsoft.com/en-us/library/ts553s52.aspx

There are two solutions one is for .NET 2.0 only and the other is general
and works on all versions.
--

Stoitcho Goutsev (100)

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP10.phx.gbl...
Joe,

First off, if you are using ASP.NET, then you really have no reason to
spawn another thread. Your page is not going to be visible while
processing anyways, so you have no need for the extra thread.

If you are using this in a richer client app, then what you want to do
is have a field in the class the method is on which indicates whether or
not it should stop processing. You need to protect access to this field
with a lock statement, so that it is not corrupted if one thread tries to
read and one thread tries to write to it.

Once you have that, check through each iteration of the loop, and if
the flag is true, then stop processing the look, and exit the method
graciously.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe Jax" <jj@nowhere.com> wrote in message
news:u6*************@TK2MSFTNGP10.phx.gbl...
I have an object that spawns a worker thread to process one of its
methods. That method processes methods on a collection of other objects.
During this processing, a user may request to cancel the entire
operation. I could request abort on the worker thread, but that is a)
potentially messy, and b) not guaranteed to take immediate effect anyway.
I would rather have some way of allowing the main thread to tell the
worker thread that it should stop. The code being executed by the worker
can then check periodically for a stop request. An added complication is
that this should be compatible with ASP.NET, meaning that any use of
static variables needs special consideration.

I'd be grateful for any advice.



Jan 7 '06 #4
The scenario is this: a user kicks off a report from a web page. The report
begins to run but may take a minute or two to generate. Meanwhile the user
decides to cancel the report. If I run the report in the main thread it will
be tied up and unable to process a request to cancel. I think. :-)

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
Joe,

First off, if you are using ASP.NET, then you really have no reason to
spawn another thread. Your page is not going to be visible while
processing anyways, so you have no need for the extra thread.

If you are using this in a richer client app, then what you want to do
is have a field in the class the method is on which indicates whether or
not it should stop processing. You need to protect access to this field
with a lock statement, so that it is not corrupted if one thread tries to
read and one thread tries to write to it.

Once you have that, check through each iteration of the loop, and if
the flag is true, then stop processing the look, and exit the method
graciously.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe Jax" <jj@nowhere.com> wrote in message
news:u6*************@TK2MSFTNGP10.phx.gbl...
I have an object that spawns a worker thread to process one of its
methods. That method processes methods on a collection of other objects.
During this processing, a user may request to cancel the entire operation.
I could request abort on the worker thread, but that is a) potentially
messy, and b) not guaranteed to take immediate effect anyway. I would
rather have some way of allowing the main thread to tell the worker thread
that it should stop. The code being executed by the worker can then check
periodically for a stop request. An added complication is that this should
be compatible with ASP.NET, meaning that any use of static variables needs
special consideration.

I'd be grateful for any advice.


Jan 7 '06 #5
Rick,

What does it matter? The user is still waiting for the request to
complete. It doesn't matter, in the ASP space, since it is a completely
differently way of processing things. To perform an asynchronous task, you
need to actually kick the task off, but it is up to the client (browser) to
refresh itself to check on the status of that task.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rick" <ri**@nospam.com> wrote in message
news:eh**************@TK2MSFTNGP11.phx.gbl...
The scenario is this: a user kicks off a report from a web page. The
report begins to run but may take a minute or two to generate. Meanwhile
the user decides to cancel the report. If I run the report in the main
thread it will be tied up and unable to process a request to cancel. I
think. :-)

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP10.phx.gbl...
Joe,

First off, if you are using ASP.NET, then you really have no reason to
spawn another thread. Your page is not going to be visible while
processing anyways, so you have no need for the extra thread.

If you are using this in a richer client app, then what you want to do
is have a field in the class the method is on which indicates whether or
not it should stop processing. You need to protect access to this field
with a lock statement, so that it is not corrupted if one thread tries to
read and one thread tries to write to it.

Once you have that, check through each iteration of the loop, and if
the flag is true, then stop processing the look, and exit the method
graciously.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe Jax" <jj@nowhere.com> wrote in message
news:u6*************@TK2MSFTNGP10.phx.gbl...
I have an object that spawns a worker thread to process one of its
methods. That method processes methods on a collection of other objects.
During this processing, a user may request to cancel the entire
operation. I could request abort on the worker thread, but that is a)
potentially messy, and b) not guaranteed to take immediate effect anyway.
I would rather have some way of allowing the main thread to tell the
worker thread that it should stop. The code being executed by the worker
can then check periodically for a stop request. An added complication is
that this should be compatible with ASP.NET, meaning that any use of
static variables needs special consideration.

I'd be grateful for any advice.



Jan 7 '06 #6
The way it works at the moment is that the client asks for the report, and
during the postback the server kicks off the report in a worker thread
(storing the report object in session state), leaving the main thread to
complete the postback and then die. The postback then includes client-side
script to instruct the browser to refresh every 3 seconds and poll the
status of the report. During this time, the user may click a cancel button,
causing a post-back. During this postback I want to stop the report from
processing any further. I therefore need some way of passing a message to
the running worker thread to ask it to stop ASAP.

One solution I have come up with is to hold a static list of all active
report worker threads. When I start a worker thread I can store the unique
thread ID both in this list and in the report object (which, remember, is
held in the session state). Then when I request to cancel the report, I can
remove the entry from the list of active threads. If each worker
periodically checks the list, it can then take action when its own entry
disappears.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eK**************@TK2MSFTNGP09.phx.gbl...
Rick,

What does it matter? The user is still waiting for the request to
complete. It doesn't matter, in the ASP space, since it is a completely
differently way of processing things. To perform an asynchronous task,
you need to actually kick the task off, but it is up to the client
(browser) to refresh itself to check on the status of that task.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rick" <ri**@nospam.com> wrote in message
news:eh**************@TK2MSFTNGP11.phx.gbl...
The scenario is this: a user kicks off a report from a web page. The
report begins to run but may take a minute or two to generate. Meanwhile
the user decides to cancel the report. If I run the report in the main
thread it will be tied up and unable to process a request to cancel. I
think. :-)

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP10.phx.gbl...
Joe,

First off, if you are using ASP.NET, then you really have no reason
to spawn another thread. Your page is not going to be visible while
processing anyways, so you have no need for the extra thread.

If you are using this in a richer client app, then what you want to
do is have a field in the class the method is on which indicates whether
or not it should stop processing. You need to protect access to this
field with a lock statement, so that it is not corrupted if one thread
tries to read and one thread tries to write to it.

Once you have that, check through each iteration of the loop, and if
the flag is true, then stop processing the look, and exit the method
graciously.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe Jax" <jj@nowhere.com> wrote in message
news:u6*************@TK2MSFTNGP10.phx.gbl...
I have an object that spawns a worker thread to process one of its
methods. That method processes methods on a collection of other objects.
During this processing, a user may request to cancel the entire
operation. I could request abort on the worker thread, but that is a)
potentially messy, and b) not guaranteed to take immediate effect
anyway. I would rather have some way of allowing the main thread to tell
the worker thread that it should stop. The code being executed by the
worker can then check periodically for a stop request. An added
complication is that this should be compatible with ASP.NET, meaning
that any use of static variables needs special consideration.

I'd be grateful for any advice.



Jan 8 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Chad Miller | last post: by
7 posts views Thread by Charles Law | last post: by
12 posts views Thread by Tomaz Koritnik | last post: by
1 post views Thread by bhargavchokshi | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.