Communicating between a main thread and a worker thread?

J

Joe Jax

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

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 said:
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]
- (e-mail address removed)

Joe Jax said:
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.
 
R

Rick

Thanks, I'll have a look at that.

Stoitcho Goutsev (100) said:
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 said:
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]
- (e-mail address removed)

Joe Jax said:
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.
 
R

Rick

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 said:
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]
- (e-mail address removed)

Joe Jax said:
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.
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Rick said:
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 said:
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]
- (e-mail address removed)

Joe Jax said:
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.
 
R

Rick

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 said:
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]
- (e-mail address removed)

Rick said:
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 said:
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]
- (e-mail address removed)

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.
 

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

Top