473,416 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 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 5971
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.