472,795 Members | 2,311 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,795 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 5906
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John | last post by:
Hi, I have a worker thread that receives messages from a socket and puts them in a queue (it sits inside a "while true") loop. Each time it adds a message to the queue, I need the main thread...
5
by: Hao L | last post by:
For example, void WorkerMethod() { ... UnregisterAllHotkeys();} void UnregisterAllHotKeys { for(...) {UnregisterHotKey(...);}} UnregisterHotKey is an API function that must be on the thread...
1
by: Chad Miller | last post by:
I know haw to call a the main thread from a worker thread in a windows form class, but how does a worker thread call its main thread from a none windows form class? Thank You. ...
7
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have...
2
by: Tim | last post by:
The are 2 threads - main thread and worker thread. The main thread creates the worker thread and then the worker thread runs in an infinite while loop till it is asked to exit by setting an event....
12
by: Tomaz Koritnik | last post by:
Is it possible to asynchronously call a method from worker thread so that this method would execute in main-thread? I'm doing some work in worker thread and would like to report progress to main...
1
by: bhargavchokshi | last post by:
Hi, I m kind of new to multithreading programming and try to implement one solution. The problem I have is, I have one background thread which does time consuming processing. The parent(calling)...
6
by: dolulob | last post by:
Hi, I'm trying to communicate with a console application through a c# program. the console application is micq a console based ICQ client. I want to be able to send an receive messages through...
1
by: EricBlair | last post by:
Hello, I've written a simple app as a test to run multiple threads from a pool. I'm able to do this, but what's happening is that the main thread finishes before all the workers do. So the...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.