473,466 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

safely stopping threads

2G
Hi

I'm trying to safely stop a thread but as you might have guessed, I'm
failing at it.
GetCurrentThread seems to get the wrong handle, my form disappears when I
try to stop the thread and it looks like the thread keeps running.
Does anyone know how to get the correct handle or a better way to stop a
thread?
Before this, I used to check a bool in the loop to see if the loop should
break but this resulted sometimes in some long waiting times for the loop to
break;

using System;

using System.Threading;

using System.Diagnostics;

using System.Runtime.InteropServices;

namespace Test

{

public delegate void ThreadStoppedHandler(object o, EventArgs e);

public delegate void ThreadStartedHandler(object o, EventArgs e);

public class ThreadControl

{

[DllImport("kernel32.dll")]

private static extern long GetExitCodeThread(long hThread, long
lpExitCode);

[DllImport("kernel32.dll")]

private static extern void ExitThread(long dwExitCode);

[DllImport("kernel32.dll")]

private static extern long GetCurrentThread();

private Thread _th;

private bool _running;

public event ThreadStoppedHandler ThreadStopped;

public event ThreadStartedHandler ThreadStarted;

public ThreadControl(ThreadStart ts) {

this._running = false;

this._th = new Thread(ts);

}

public bool IsRunning {

get{ return this._running; }

}

public ThreadPriority Priority {

get{ return this._th.Priority; }

set{ this._th.Priority = value; }

}

public void Start() {

if(!this._running) {

this._th.Start();

this._running = true;

if(this.ThreadStarted!=null)

this.ThreadStarted(this, new EventArgs());

} else {

this.Stop();

this.Start();

}

}

public void Stop(){

if(this._running)

ExitThread(GetExitCodeThread(GetCurrentThread(), 0));

this._running = false;

if(this.ThreadStopped!=null)

this.ThreadStopped(this, new EventArgs());

}

}

}

thanks
Nov 16 '05 #1
0 1099

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

Similar topics

1
by: Russell E. Owen | last post by:
I've been doing some socket programming with threads and am confused how to safely shut down a connection. First of all, using the file-like object returned by urllib to do ftp file download...
6
by: Fabiano Sidler | last post by:
Hello Newsgroup! In my Python script, I use the 'thread' module (not 'threading') and 'signal' simultaneously. All spawned threads execute 'pcapObject.loop(-1, callback)', which does not return....
1
by: Sam Martin | last post by:
hi all, on the ServiceBase.OnStart i'm loading setting, etc. if the info i need is not available, i'm writing to the event log, and want to manually end the service. aside from exiting the...
4
by: Frank Rizzo | last post by:
Hello, I want to use a variable as a signal to the thread that it should stop what it's doing (code below). As a rule, should I lock an object only when I am writing to it or do I have to lock...
4
by: MSDousti | last post by:
Hi I have written a VB .NET app, which uses several threads. I thought that when the user closes the main window (when MainForm.closed event occures, and I call application.exit) all running...
11
by: Steve | last post by:
I'm having a problem with my Thread usage and I think the general design of how I'm working with them. My UI class calls a method in another class that does a lot of work. That "worker" class...
2
by: amadeusz.jasak | last post by:
Hello, it is possible to stop all threads (application) from thread of application: App |-MainThread |-WebServer |-CmdListener # From this I want to stop App The sys.exit isn't working...
1
by: mclaugb | last post by:
Here is a simple piece of thread code. When i add the print (or other) function into the run method--the thread fails to stop after 2 seconds which the join(2) should ensure. I have a function...
3
by: =?Utf-8?B?c21rcmFtZXIwNzJmZGFzZmFzZGY=?= | last post by:
This questions is in reference to the article on Using Events in the C# Programmers Reference (http://msdn2.microsoft.com/en-us/library/ms173168.aspx). Under the Rasing Events section it says in...
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...
1
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
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.