473,419 Members | 1,665 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,419 software developers and data experts.

Terminate A Long Running Method

Dan
Good Day,

I have a Windows Service that is responsible for processing somes files. One
method of the service invokes another method which may take a while to
complete its work. Something like this

pulbic class A
{

public void ProcessFile()
{

B Test = new B();
Test.DoWork();

}

}

public class B
{

public void DoWork()
{
//Long Running Method
}

}

Here is my problem. I need a way to signal the DoWork method that the
service has been called to stop and it need to finish whatever it is doing
gracefully and exit. So far the only solution I can come up with is to launch
the method on its own seperate thread. Then, the other method can monitor the
thread until it is done. If it needs to stop it can set a flag on the object
telling it to stop processing.

Is this really the best way to do this? Is there another way to achieve this
without launching a seperate thread?

Just looking for ideas. Thanks!

Dan

Nov 16 '05 #1
1 2285
No I think that a thread is your only answer.

However there are a couple of options for the threads. You could
1) Use a .Net worker thread from the thread pool.
2) Create a class to wrap the thread and have a Stop property that
something else could set to tell the long running method to stop itself.
This assumes that the method can periodically check the value of the Stop
property. Otherwhise you'll have to take the brute force kill the thread
approach.

I like the second option myself but it really does depend upon what the long
running method is doing. The stuff I write tends to be protocol converters
so I have a thread monitoring a TCP connection whating for input which is
then translated into a different format and passed to another thread to be
sent out on a different connection. If you long running method is being used
to read from a database for instance or just to send a sing message and
process the response then a thread form the thread pool is probably better.

Paul.
"Dan" <Da*@discussions.microsoft.com> wrote in message
news:BE**********************************@microsof t.com.resend...
Good Day,

I have a Windows Service that is responsible for processing somes files. One method of the service invokes another method which may take a while to
complete its work. Something like this

pulbic class A
{

public void ProcessFile()
{

B Test = new B();
Test.DoWork();

}

}

public class B
{

public void DoWork()
{
//Long Running Method
}

}

Here is my problem. I need a way to signal the DoWork method that the
service has been called to stop and it need to finish whatever it is doing
gracefully and exit. So far the only solution I can come up with is to launch the method on its own seperate thread. Then, the other method can monitor the thread until it is done. If it needs to stop it can set a flag on the object telling it to stop processing.

Is this really the best way to do this? Is there another way to achieve this without launching a seperate thread?

Just looking for ideas. Thanks!

Dan

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dr. J | last post by:
How to terminate a blocked thread? In my form's "load" I launch a TCP listening thread that stays in an infinite loop waiting for incoming TCP packets. In this form's "closing" I try to...
3
by: Kathy Burke | last post by:
Hi, I'm tired, so this question may be silly. I have a fairly long sub procedure. Based on one condition, I load another sub with the following: If Session("GRN") = "complete" Then txtScan.Text...
3
by: Peter Neuburger via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Peter Neuburger Hi Everybody, I need some help with the WMI Terminate Method. I have a Listbox where I get all the Process...
9
by: Li Pang | last post by:
Hi I make an app which can run some sub processes through multiple threads. I'd like to know how to terminate all sub-threads when the main thread is closed thanks in advance
0
by: Benoit Martin | last post by:
here is my code: //*** snippet start string arg = ""; WorkDelegate d = new WorkDelegate(Printing); IAsyncResult res = d.BeginInvoke(arg,null,null); if(res.IsCompleted == false) {...
1
by: Anonieko | last post by:
Query: How to display progress bar for long running page Answer: Yet another solution. REFERENCE: http://www.eggheadcafe.com/articles/20050108.asp My only regret is that when click the...
0
by: archana | last post by:
Hi all, I am having problem in one web method which i am running asynchronously I set oneway attribute in web method. What i want is once client called this web method this should start...
5
by: david | last post by:
I'm developing a program that runs using an asyncore loop. Right now I can adequately terminate it using Control-C, but as things get refined I need a better way to stop it. I've developed...
0
by: norseman | last post by:
Gros Bedo wrote: ============================== Yes. man ps explains try ps -AFL | grep then kill -9 found (check it more than twice) 1) If your script is known to hang use what...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
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.