killing and pausing multiple threads

rfazendeiro

Centurion
Joined
Mar 8, 2004
Messages
110
hi ti all,

i have 3 files that are being converted to another format. in the end there are still 3 files so i was thinking it would be better to make a thread to treat them "simultaneously".

I new to threading so i would like some help. I have 3 methods that i use to inicialice the threads

private void Process1()
private void Process2()
private void Process3()

Code:
private void btnLerFich_Click(object sender, System.EventArgs e)
{
	this.trd1 = new Thread(new ThreadStart(Process1));
	this.trd1 .Name = "Process1";

	this.trd2= new Thread(new ThreadStart(Process2));
	this.trd2.Name = "Process2";

	this.trd3 = new Thread(new ThreadStart(Process3));
	this.trd3 .Name = "Process3";
			
	this.trd1 .Start();
	this.trd2.Start();
	this.trd3 .Start();
}

now i have 3 buttons. button1 allow the user to stop the entire process and kill all thread. button 2 allows the user to pause the all processes and then button3 allows process to resume.

Now how can i do this? how can i stop all threads and pause all thread?

thanks for the help
 
yes....that i know.

but i was asking is since i have multiple threa running and i want to stop them all. Is there a way to go through all existing thread?? or i do My.Thread.Suspend to all when the user clicks the button? i only have 2 threads now but i could have more and it's not practical to do that
 
Generally pausing and stopping threads is a bad move.
A better/safer approach would be to use System.Threading.Monitor to control your threads from the main thread.
 
But System.Threading.Monitor is to sincronize threads. i want them to run concurrently. I just want the user to be able to stop the process. There's got to be a way to just kill all threads. Plz help
 
Create a form-level collection.

As you create each thread, add it to the collection.

When you want to pause/resume the threads, iterate through the collection and pause/resume each one.

Alternatively, inherit the Collection class and create a Typed collection. Make two methods in the collection: PauseAll() and ResumeAll() Make those methods iterate through the inner list and pause/resume the threads. Then you can simply call:

yourProcessCollection.PauseAll()

or

yourProcessCollection.ResumeAll()

(you still have to add eath thread to the collection when you create it)

B.
 
What are the threads doing? Unless they are waiting you do nothing be increase complexity by having a single thread per process. If they are actually processing data and not listening for network messages or events or reading from the fielsystem then you will be better off doing the actions in series rather than in parallel.

You might also want to look at the ThreadPool which is provided specifically to keep a small number of thread busy and working on user provided activities.
 
Knocked up a quickie, VB sample. (Sorry, I'm not a C# man!)

Should cover most of the basics, but its:

a) untested
b) uncompiled
c) forumed-code

Cheers!

Visual Basic:
Imports System.Threading
Public Class Form1
	Inherits System.Windows.Forms.Form

	Dim tc As New ThreadCollection

#Region " Windows Form Designer generated code "

	Public Sub New()
		MyBase.New()

		'This call is required by the Windows Form Designer.
		InitializeComponent()

		'Add any initialization after the InitializeComponent() call

	End Sub

	'Form overrides dispose to clean up the component list.
	Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
		If disposing Then
			If Not (components Is Nothing) Then
				components.Dispose()
			End If
		End If
		MyBase.Dispose(disposing)
	End Sub

	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer

	'NOTE: The following procedure is required by the Windows Form Designer
	'It can be modified using the Windows Form Designer.  
	'Do not modify it using the code editor.
	Friend WithEvents cmdStart As System.Windows.Forms.Button
	Friend WithEvents cmdResume As System.Windows.Forms.Button
	Friend WithEvents cmdPause As System.Windows.Forms.Button
	Friend WithEvents cmdStop As System.Windows.Forms.Button
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.cmdStart = New System.Windows.Forms.Button
		Me.cmdPause = New System.Windows.Forms.Button
		Me.cmdResume = New System.Windows.Forms.Button
		Me.cmdStop = New System.Windows.Forms.Button
		Me.SuspendLayout()
		'
		'cmdStart
		'
		Me.cmdStart.Location = New System.Drawing.Point(72, 156)
		Me.cmdStart.Name = "cmdStart"
		Me.cmdStart.Size = New System.Drawing.Size(108, 23)
		Me.cmdStart.TabIndex = 0
		Me.cmdStart.Text = "Start"
		'
		'cmdPause
		'
		Me.cmdPause.Location = New System.Drawing.Point(72, 188)
		Me.cmdPause.Name = "cmdPause"
		Me.cmdPause.Size = New System.Drawing.Size(108, 23)
		Me.cmdPause.TabIndex = 1
		Me.cmdPause.Text = "Pause"
		'
		'cmdResume
		'
		Me.cmdResume.Location = New System.Drawing.Point(72, 224)
		Me.cmdResume.Name = "cmdResume"
		Me.cmdResume.Size = New System.Drawing.Size(108, 23)
		Me.cmdResume.TabIndex = 2
		Me.cmdResume.Text = "Resume"
		'
		'cmdStop
		'
		Me.cmdStop.Location = New System.Drawing.Point(72, 256)
		Me.cmdStop.Name = "cmdStop"
		Me.cmdStop.Size = New System.Drawing.Size(108, 23)
		Me.cmdStop.TabIndex = 3
		Me.cmdStop.Text = "Stop"
		'
		'Form1
		'
		Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
		Me.ClientSize = New System.Drawing.Size(488, 346)
		Me.Controls.Add(Me.cmdStop)
		Me.Controls.Add(Me.cmdResume)
		Me.Controls.Add(Me.cmdPause)
		Me.Controls.Add(Me.cmdStart)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)

	End Sub

#End Region

	Private Sub Process1()
		' User defined stuff here
	End Sub

	Private Sub Process2()
		' User Defined Stuff here
	End Sub

	Private Sub Process3()
		' User defined stuff here
	End Sub

	Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
		Dim p As New Thread(New ThreadStart(AddressOf Process1))
		p.Name = "Process1"
		tc.Add(p)

		p = New Thread(New ThreadStart(AddressOf Process2))
		p.Name = "Process2"
		tc.Add(p)

		p = New Thread(New ThreadStart(AddressOf Process3))
		p.Name = "Process3"
		tc.Add(p)
	End Sub

	Private Sub cmdPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPause.Click
		tc.SuspendAll()
	End Sub

	Private Sub cmdResume_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdResume.Click
		tc.ResumeAll()
	End Sub

	Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
		tc.StopAll()
	End Sub

End Class

Public Class ThreadCollection
	Inherits CollectionBase

	Public Sub ResumeAll()
		For Each p As Thread In Innerlist
			If p.ThreadState = ThreadState.Suspended Then
				p.Resume()
			End If
		Next
	End Sub

	Public Sub SuspendAll()
		For Each p As Thread In Innerlist
			If p.ThreadState = ThreadState.Running Then
				p.Suspend()
			End If
		Next
	End Sub

	Public Sub StopAll()
		For Each p As Thread In Innerlist
			p.Abort()
			' probably need some other cleanup. Perhaps dispose of each
			' thread and remove from the collection.
		Next
	End Sub

	Public Function Add(ByVal p As Thread) As Integer
		Return Innerlist.Add(p)
	End Function

	Public Function Remove(ByVal p As Thread)
		Innerlist.Remove(p)
	End Function

	Public Function RemoveRange(ByVal index As Integer, ByVal count As Integer)
		innerlist.RemoveRange(index, count)
	End Function

	Default Public Property Item(ByVal index As Integer) As Thread
		Get
			If index < 0 Or index > innerlist.Count Then
				Throw New IndexOutOfRangeException
			Else
				Return DirectCast(innerlist.Item(index), Thread)
			End If
		End Get
		Set(ByVal Value As Thread)
			innerlist.Item(index) = Value
		End Set
	End Property
End Class
 
hi,

i have implemented in c#. It has a few problems but this is what i was looking for! thx a lot

Thx for al replies
 
Back
Top