473,545 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2305
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*@discussion s.microsoft.com > wrote in message
news:BE******** *************** ***********@mic rosoft.com.rese nd...
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
4926
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 terminate this thread by calling the "Abort" funcion. But the thread does not terminate and after the form is closed this thread keeps running at blocked...
3
2456
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 = Session("SN") txtScan_TextChanged(sender, e) Session("GRN") = "" Exit Sub End If
3
5915
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 Information using the WMI Win32_process
9
7409
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
1005
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) { res.AsyncWaitHandle.WaitOne(5000,false); if(res.IsCompleted == false) {
1
6003
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 browser back button, it loads the progress bar again. Any solution to this?
0
1467
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 asynchronously. My client is windows application means after calling web method
5
6631
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 another program that executes it as a child process using popen2.Popen4(). I was attempting to use signals to stop it (using os.kill()) but I keep...
0
2001
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 another wrote: py s.py tokens & capture=$! (get the PID ready)
0
7432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7689
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7456
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5359
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.