473,399 Members | 3,603 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,399 software developers and data experts.

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 1289
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.