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

Multithread Stop/Restart

Hi all,

I have 3 running threads chencking for something in my application.
Based on that I need to stop some of them safely wait for something
else and restart. Sleep is not good in my case because i don't know how
much to wait. How and from where I can put a thread in pause an restart
it?

Thank you
Juky
Sub main ()
....
t1 = New Threading.Thread(AddressOf Check1)
t1.Start()
t2 = New Threading.Thread(AddressOf Check2)
t2.Start()
t3 = New Threading.Thread(AddressOf Check3)
t3.Start()
end sub

sub Check1()
while true
"IF CHECK=TRUE THEN I HAVET TO SUSPEND THIS THREAD
end while
end Sub

sub Check2()
while true
"IF CHECK=TRUE THEN I HAVET TO SUSPEND THIS THREAD
end while
end Sub

sub Check3()
while true
"IF CHECK=TRUE THEN I HAVET TO SUSPEND THIS THREAD
end while
end Sub

Nov 21 '05 #1
5 2084
Juky,

Are you sure you need those threads.

Remember that the processing time of an applications with multithreading is
longer than withouth that.

When there are a lot of dependencies that can be as well for the throughput
time.

Cor
Nov 21 '05 #2
One way to do it is to have an appropriately scoped boolean variable (check)
and set/reset it as required in your external code. In you thread(s), handle
the condition thus:

Sub Check1()

Do

If Not check Then

' Do whatever needs doing

End If

Loop

End Sub

Whatever processing the thread does will effectively be suspended while
check it set.
Note that if check is set while the thread is actually doing it's processing
then the 'suspension' will not be effective until the next iteration of the
loop, but this should give you the general idea.
"juky" <ju*******@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi all,

I have 3 running threads chencking for something in my application.
Based on that I need to stop some of them safely wait for something
else and restart. Sleep is not good in my case because i don't know how
much to wait. How and from where I can put a thread in pause an restart
it?

Thank you
Juky
Sub main ()
....
t1 = New Threading.Thread(AddressOf Check1)
t1.Start()
t2 = New Threading.Thread(AddressOf Check2)
t2.Start()
t3 = New Threading.Thread(AddressOf Check3)
t3.Start()
end sub

sub Check1()
while true
"IF CHECK=TRUE THEN I HAVET TO SUSPEND THIS THREAD
end while
end Sub

sub Check2()
while true
"IF CHECK=TRUE THEN I HAVET TO SUSPEND THIS THREAD
end while
end Sub

sub Check3()
while true
"IF CHECK=TRUE THEN I HAVET TO SUSPEND THIS THREAD
end while
end Sub

Nov 21 '05 #3
"juky" <ju*******@yahoo.com> schrieb:
I have 3 running threads chencking for something in my application.
Based on that I need to stop some of them safely wait for something
else and restart. Sleep is not good in my case because i don't know how
much to wait. How and from where I can put a thread in pause an restart
it?


The sample below will start a thread that waits (blocks the thread) until an
event occurs:

\\\
Imports System.Threading

Public Module Program
Private SampleTest As New Test()

Public Sub Main()
Dim t As New Thread(AddressOf SampleTest.Foo)
t.Start()
Console.ReadLine()
SampleTest.EventHandler()
End Sub
End Module

Public Class Test
Private EventOccured As New ManualResetEvent(False)

Public Sub Foo()
Debug.WriteLine("Started!")
EventOccured.WaitOne(-1, False)
Debug.WriteLine("Event occured!")
End Sub

Public Sub EventHandler()
EventOccured.Set()
End Sub
End Class
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
Juky,
As Herfried suggests I would use a ManualResetEvent.

Here is a an example I posted a few weeks ago of a Worker class using a
ManualResetEvent to control suspending & resuming.

Public Class Worker

Public Delegate Sub Work()

Private ReadOnly m_work As Work
Private ReadOnly m_arg As Object
Private ReadOnly m_event As ManualResetEvent
Private ReadOnly m_thread As Thread

<ThreadStatic()> _
Private Shared m_current As Worker

Public Sub New(ByVal work As Work, ByVal arg As Object, ByVal name
As String)
m_work = work
m_arg = arg
m_event = New ManualResetEvent(True)
m_thread = New Thread(AddressOf Start)
m_thread.Name = name
m_thread.IsBackground = True
m_thread.Start()
End Sub

Private Sub Start()
m_current = Me
m_work.Invoke()
End Sub

Public Shared ReadOnly Property CurrentWorker() As Worker
Get
Return m_current
End Get
End Property

Public ReadOnly Property Arg() As Object
Get
Return m_arg
End Get
End Property

Public ReadOnly Property Name() As String
Get
Return m_thread.Name
End Get
End Property

Public Sub Suspend()
If Thread.CurrentThread Is m_thread Then
Throw New InvalidOperationException("Suspend should not be
called from the worker thread!")
End If
m_event.Reset()
End Sub

Public Sub [Resume]()
If Thread.CurrentThread Is m_thread Then
Throw New InvalidOperationException("Resume should not be
called from the worker thread!")
End If
m_event.Set()
End Sub

Public Sub WaitForTermination()
If Thread.CurrentThread Is m_thread Then
Throw New InvalidOperationException("WaitForTermination
should not be called from the worker thread!")
End If
m_thread.Join()
End Sub

Public Sub CheckSuspend()
If Not Thread.CurrentThread Is m_thread Then
Throw New InvalidOperationException("CheckSuspend should
only be called from the worker thread!")
End If
m_event.WaitOne()
End Sub

End Class

To see how it works try something like:

Private Sub Work()
For index As Integer = 1 To 50
Worker.CurrentWorker.CheckSuspend()
Debug.WriteLine(index, Worker.CurrentWorker.Name)
Dim value As Double = DirectCast(Worker.CurrentWorker.Arg,
Double)
Thread.Sleep(TimeSpan.FromSeconds(value))
Next
End Sub

Public Sub Main()
Dim worker1 As New Worker(AddressOf Work, 0.25, "worker1")
Dim worker2 As New Worker(AddressOf Work, 0.5, "worker2")
Dim worker3 As New Worker(AddressOf Work, 0.75, "worker3")

Debug.WriteLine("Pausing 5 seconds", "Main")
Thread.Sleep(TimeSpan.FromSeconds(5))
worker1.Suspend()

Debug.WriteLine("Pausing 5 seconds", "Main")
Thread.Sleep(TimeSpan.FromSeconds(5))
worker2.Suspend()

Debug.WriteLine("Pausing 5 seconds", "Main")
Thread.Sleep(TimeSpan.FromSeconds(5))
worker3.Suspend()

Debug.WriteLine("Pausing 1 seconds", "Main")
Thread.Sleep(TimeSpan.FromSeconds(1))

worker1.Resume()
worker2.Resume()
worker3.Resume()

Debug.WriteLine("Waiting for Termination", "Main")
worker1.WaitForTermination()
worker2.WaitForTermination()
worker3.WaitForTermination()

End Sub

The above sample was posted in a thread titled "Technique for Pausing Worker
Thread" in this newsgroup between 21 Jan 2005 & 23 Jan 2005.

Hope this helps
Jay
"juky" <ju*******@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi all,

I have 3 running threads chencking for something in my application.
Based on that I need to stop some of them safely wait for something
else and restart. Sleep is not good in my case because i don't know how
much to wait. How and from where I can put a thread in pause an restart
it?

Thank you
Juky
Sub main ()
....
t1 = New Threading.Thread(AddressOf Check1)
t1.Start()
t2 = New Threading.Thread(AddressOf Check2)
t2.Start()
t3 = New Threading.Thread(AddressOf Check3)
t3.Start()
end sub

sub Check1()
while true
"IF CHECK=TRUE THEN I HAVET TO SUSPEND THIS THREAD
end while
end Sub

sub Check2()
while true
"IF CHECK=TRUE THEN I HAVET TO SUSPEND THIS THREAD
end while
end Sub

sub Check3()
while true
"IF CHECK=TRUE THEN I HAVET TO SUSPEND THIS THREAD
end while
end Sub

Nov 21 '05 #5
I missed an addendum to the original thread:

An alternate Worker.Suspend implementation might be:

Public Sub Suspend()
m_event.Reset()
If Thread.CurrentThread Is m_thread Then
m_event.WaitOne()
End If
End Sub

Which allows the worker thread to suspend itself.

I would also consider implementing CurrentWorker as:

Public Shared ReadOnly Property CurrentWorker() As Worker
Get
If m_current Is Nothing Then
Throw New InvalidOperationException("CurrentWorker
should only be called from a Worker thread!")
End If
Return m_current
End Get
End Property

To ensure that it is only called from Worker Threads (preventing
NullReferenceExceptions in non-worker threads!).

Hope this helps
Jay
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uf**************@TK2MSFTNGP15.phx.gbl...
Juky,
As Herfried suggests I would use a ManualResetEvent.

Here is a an example I posted a few weeks ago of a Worker class using a
ManualResetEvent to control suspending & resuming.

Public Class Worker

Public Delegate Sub Work()

Private ReadOnly m_work As Work
Private ReadOnly m_arg As Object
Private ReadOnly m_event As ManualResetEvent
Private ReadOnly m_thread As Thread

<ThreadStatic()> _
Private Shared m_current As Worker

Public Sub New(ByVal work As Work, ByVal arg As Object, ByVal name
As String)
m_work = work
m_arg = arg
m_event = New ManualResetEvent(True)
m_thread = New Thread(AddressOf Start)
m_thread.Name = name
m_thread.IsBackground = True
m_thread.Start()
End Sub

Private Sub Start()
m_current = Me
m_work.Invoke()
End Sub

Public Shared ReadOnly Property CurrentWorker() As Worker
Get
Return m_current
End Get
End Property

Public ReadOnly Property Arg() As Object
Get
Return m_arg
End Get
End Property

Public ReadOnly Property Name() As String
Get
Return m_thread.Name
End Get
End Property

Public Sub Suspend()
If Thread.CurrentThread Is m_thread Then
Throw New InvalidOperationException("Suspend should not be
called from the worker thread!")
End If
m_event.Reset()
End Sub

Public Sub [Resume]()
If Thread.CurrentThread Is m_thread Then
Throw New InvalidOperationException("Resume should not be
called from the worker thread!")
End If
m_event.Set()
End Sub

Public Sub WaitForTermination()
If Thread.CurrentThread Is m_thread Then
Throw New InvalidOperationException("WaitForTermination
should not be called from the worker thread!")
End If
m_thread.Join()
End Sub

Public Sub CheckSuspend()
If Not Thread.CurrentThread Is m_thread Then
Throw New InvalidOperationException("CheckSuspend should
only be called from the worker thread!")
End If
m_event.WaitOne()
End Sub

End Class

To see how it works try something like:

Private Sub Work()
For index As Integer = 1 To 50
Worker.CurrentWorker.CheckSuspend()
Debug.WriteLine(index, Worker.CurrentWorker.Name)
Dim value As Double = DirectCast(Worker.CurrentWorker.Arg,
Double)
Thread.Sleep(TimeSpan.FromSeconds(value))
Next
End Sub

Public Sub Main()
Dim worker1 As New Worker(AddressOf Work, 0.25, "worker1")
Dim worker2 As New Worker(AddressOf Work, 0.5, "worker2")
Dim worker3 As New Worker(AddressOf Work, 0.75, "worker3")

Debug.WriteLine("Pausing 5 seconds", "Main")
Thread.Sleep(TimeSpan.FromSeconds(5))
worker1.Suspend()

Debug.WriteLine("Pausing 5 seconds", "Main")
Thread.Sleep(TimeSpan.FromSeconds(5))
worker2.Suspend()

Debug.WriteLine("Pausing 5 seconds", "Main")
Thread.Sleep(TimeSpan.FromSeconds(5))
worker3.Suspend()

Debug.WriteLine("Pausing 1 seconds", "Main")
Thread.Sleep(TimeSpan.FromSeconds(1))

worker1.Resume()
worker2.Resume()
worker3.Resume()

Debug.WriteLine("Waiting for Termination", "Main")
worker1.WaitForTermination()
worker2.WaitForTermination()
worker3.WaitForTermination()

End Sub

The above sample was posted in a thread titled "Technique for Pausing
Worker Thread" in this newsgroup between 21 Jan 2005 & 23 Jan 2005.

Hope this helps
Jay
"juky" <ju*******@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi all,

I have 3 running threads chencking for something in my application.
Based on that I need to stop some of them safely wait for something
else and restart. Sleep is not good in my case because i don't know how
much to wait. How and from where I can put a thread in pause an restart
it?

Thank you
Juky
Sub main ()
....
t1 = New Threading.Thread(AddressOf Check1)
t1.Start()
t2 = New Threading.Thread(AddressOf Check2)
t2.Start()
t3 = New Threading.Thread(AddressOf Check3)
t3.Start()
end sub

sub Check1()
while true
"IF CHECK=TRUE THEN I HAVET TO SUSPEND THIS THREAD
end while
end Sub

sub Check2()
while true
"IF CHECK=TRUE THEN I HAVET TO SUSPEND THIS THREAD
end while
end Sub

sub Check3()
while true
"IF CHECK=TRUE THEN I HAVET TO SUSPEND THIS THREAD
end while
end Sub


Nov 21 '05 #6

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

Similar topics

4
by: Niel | last post by:
Hello friends, I think this issue has been posted before but i have not been able to get any solution that is why i am posting it again in detail as to what exactly is happneing in my case. Hope...
0
by: r_obert | last post by:
Hello, I'm trying to create a worker thread for my VC++ program, and was wondering whether I should be linking with the Multithread /MT or Multithread DLL /MD option? I'm not quite sure, in...
1
by: stash | last post by:
When I stop all DB2 services on our Unix box - I still can see the following two DB2 processes running: root 20410 1 0 Oct 28 - 4:16 /usr/opt/db2_08_01/bin/db2fmcd db2as 21780 ...
4
by: zbcong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a...
1
by: Primera | last post by:
I have an application that fixes some common problems that prevent the SMS Advanced Client from operating correctly. During this application I need to stop the Windows Management Instrumentation...
7
by: shai | last post by:
I am working at .net 1.1, writing in c#. I have windows service with a COM object. Every unexpected time The COM object throw an error that make my service get stuck (do not respond). I can catch...
0
by: Emanuele | last post by:
I have write a program using MS Visual studio C++ 7.0 (platform Windows XP professional). I'm not using .NET. This program save data in a SQL server 2000 database using ADO. Everything works...
2
by: tikcireviva | last post by:
Hi Guys, I've done a mulithread queue implementation on stl<queue>, my developement environment is on VC6 as well as FC3. Let's talks about the win32 side. The suspected memory leak is find...
1
by: aj | last post by:
A few service stop/start/restart questions on SQL Server 2005 SP2, which I'll call SQLS. It looks as if there are *potentially* 6 ways to start/stop SQLS Services like the engine itself,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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
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...
0
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,...
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...

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.