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

Home Posts Topics Members FAQ

Thread problem

This is the first time I have tried to use multiple threads in an
application so I hope someone can help me.
I am using a second thread to do some work but I need to be able to cancel
the thread at any time. I have implemented the thread as below.
The problem I have is that usually when I call the CancelThread routine
while the thread is running the application hangs.
I tried putting a break point at WUThreadisFinished.Set().
Normally (without calling the CancelThread) the break point is reached. But
usually when I call the CancelThread the break point is never reached.
How can this be. I thought if the code is in the 'finally' part of a try
statement then that code cannot be bypassed.
The hang occurs at WUThreadisFinished.WaitOne() line and it seems the thread
somehow stops without doing WUThreadisFinished.Set().

What am I doing wrong?

Thanks
Fred
Private CancelWUThread As New System.Threading.ManualResetEvent(False)
Private WUThreadisFinished As New System.Threading.ManualResetEvent(True)

Private Sub StartThread()
Dim th As New System.Threading.Thread(AddressOf DoSomeWork)
CancelWUThread.Reset()
WUThreadisFinished.Reset()
th.Start()
End Sub

Private Sub CancelThread()
If CancelWUThread.Set() Then
WUThreadisFinished.WaitOne()
End If
End Sub

Private Sub DoSomeWork()
Try
'
'Doing some work here
'
Do
'
'Do some more work here
'
Loop Until CancelWUThread.WaitOne(0, False) Or ...
Finally
WUThreadisFinished.Set()
End Try
End Sub

Nov 20 '05 #1
2 1290
Cor
Hi Fred,

I do not see an instruction mythread.abort which is to cancel a thread in
your code.

Cor
This is the first time I have tried to use multiple threads in an
application so I hope someone can help me.
I am using a second thread to do some work but I need to be able to cancel
the thread at any time. I have implemented the thread as below.
The problem I have is that usually when I call the CancelThread routine
while the thread is running the application hangs.
I tried putting a break point at WUThreadisFinished.Set().
Normally (without calling the CancelThread) the break point is reached. But usually when I call the CancelThread the break point is never reached.
How can this be. I thought if the code is in the 'finally' part of a try
statement then that code cannot be bypassed.
The hang occurs at WUThreadisFinished.WaitOne() line and it seems the thread somehow stops without doing WUThreadisFinished.Set().

What am I doing wrong?

Thanks
Fred
Private CancelWUThread As New System.Threading.ManualResetEvent(False)
Private WUThreadisFinished As New System.Threading.ManualResetEvent(True)

Private Sub StartThread()
Dim th As New System.Threading.Thread(AddressOf DoSomeWork)
CancelWUThread.Reset()
WUThreadisFinished.Reset()
th.Start()
End Sub

Private Sub CancelThread()
If CancelWUThread.Set() Then
WUThreadisFinished.WaitOne()
End If
End Sub

Private Sub DoSomeWork()
Try
'
'Doing some work here
'
Do
'
'Do some more work here
'
Loop Until CancelWUThread.WaitOne(0, False) Or ...
Finally
WUThreadisFinished.Set()
End Try
End Sub

Nov 20 '05 #2
When I cancel the thread I want to immediately restart it hence I send a
Cancel signal (CancelWUThread.Set()) to the thread then wait for it to
finish by waiting for the thread to execute WUThreadisFinished.Set().
The trouble with the abort method is that you don't know when the thread is
actually finished so how do you know when it is safe to restart the thread.

Thanks
Fred

"Cor" <no*@non.com> wrote in message
news:ut**************@TK2MSFTNGP09.phx.gbl...
Hi Fred,

I do not see an instruction mythread.abort which is to cancel a thread in
your code.

Cor
This is the first time I have tried to use multiple threads in an
application so I hope someone can help me.
I am using a second thread to do some work but I need to be able to cancel the thread at any time. I have implemented the thread as below.
The problem I have is that usually when I call the CancelThread routine
while the thread is running the application hangs.
I tried putting a break point at WUThreadisFinished.Set().
Normally (without calling the CancelThread) the break point is reached.

But
usually when I call the CancelThread the break point is never reached.
How can this be. I thought if the code is in the 'finally' part of a try
statement then that code cannot be bypassed.
The hang occurs at WUThreadisFinished.WaitOne() line and it seems the

thread
somehow stops without doing WUThreadisFinished.Set().

What am I doing wrong?

Thanks
Fred
Private CancelWUThread As New System.Threading.ManualResetEvent(False)
Private WUThreadisFinished As New System.Threading.ManualResetEvent(True)
Private Sub StartThread()
Dim th As New System.Threading.Thread(AddressOf DoSomeWork)
CancelWUThread.Reset()
WUThreadisFinished.Reset()
th.Start()
End Sub

Private Sub CancelThread()
If CancelWUThread.Set() Then
WUThreadisFinished.WaitOne()
End If
End Sub

Private Sub DoSomeWork()
Try
'
'Doing some work here
'
Do
'
'Do some more work here
'
Loop Until CancelWUThread.WaitOne(0, False) Or ...
Finally
WUThreadisFinished.Set()
End Try
End Sub


Nov 20 '05 #3

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

Similar topics

6
by: Tony Proctor | last post by:
Hi everyone We're experiencing some serious anomalies with the scheduling of ASP threads. I'd be interested to hear if anyone knows what algorithm is used (e.g. simple round-robin, or something...
7
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
6
by: Tomaz Koritnik | last post by:
I have a class that runs one of it's method in another thread. I use Thread object to do this and inside ThreadMethod I have an infinite loop: While (true) { // do something Thread.Sleep(100);...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
4
by: fred | last post by:
I use a Synclock in a secondary thread and also stop the thread using the abort method. If the abort occurs while the thread is in the Synclock will the SyncLock always be released before the...
51
by: Hans | last post by:
Hi all, Is there a way that the program that created and started a thread also stops it. (My usage is a time-out). E.g. thread = threading.Thread(target=Loop.testLoop) thread.start() ...
14
by: joey.powell | last post by:
I am using VS2005 for a windows forms application. I need to be able to use a worker thread function to offload some processing from the UI thread. The worker thread will need access to a...
7
by: Sin Jeong-hun | last post by:
Hi. I'm writing a Client/Multi-threaded Server program on Windows Vista. It worked fine on Windows Vista, but when the server ran on Windows XP, I/O operation has been aborted because of either...
34
by: Creativ | last post by:
Why does Thread class not support IDisposable? It's creating quite some problem. Namely, it can exhaust the resource and you have not control over it.
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
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...
1
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: 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.