Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to create, start, stop, suspend and resume thread using Thread class in VB.net - C#.net Part1

Author: Shehzad Hemani
Download Source Code : 1049_source.zip

In this simple article you will learn that how we can create, start, stop, suspend and resume thread using Thread class in VB and C#.net.

 

Creating Thread:
To create thread we have to use two classes one is Thread and second is ThreadStart class. To create thread first of all create an object of thread class. Constructor of thread class takes an object of ThreadStart class. ThreadStart class object takes the name of function that will run under that thread. After creating the ThreadStart object pass that object to thread class object. Now the thread has created.
To demonstrate make a window application. Drag one button on form.
Declare two objects.
C#
Thread t;
ThreadStart st;
VB
Dim As Thread
Dim st As ThreadStart
Now write the following code on Button click event:
C#
private void btn_createThread_Click(object sender, EventArgs e)
        {
            st = new ThreadStart(countFunction);
            t = new Thread(st);
            MessageBox.Show("Thread has created");
        }
VB
Private Sub btn_createThread_Click(ByVal sender As ObjectByVal As EventArgs)
        st 
= New ThreadStart(countFunction)
        t 
= New Thread(st)
        MessageBox.Show(
"Thread has created")
    
End Sub
Function code:
C#
void countFunction()
        {
            while (true)
            {
                count++;
            }
        }
VB
Private Sub countFunction()
        
        
While true
            
count (count + 1)
            
        
End While
    End Sub
This simple code creates a thread.
Start Thread:
After creating thread, it doesn’t start work till you do not start it. To start the thread we have a function of thread class named start (). This function takes and returns nothing. After starting thread, it starts working parallel. In this thread that function will call which is passed in ThreadStart constructor.
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_Start_Click(object sender, EventArgs e)
        {
            try
            {
                t.Start();
                MessageBox.Show("Thread has started");
            }catch(Exception ex){}
        }
VB
Private Sub btn_Start_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            
t.Start
            MessageBox.Show(
"Thread has started")
        
Catch ex As Exception
            
        End Try
    End Sub
This simple code starts the thread.
Stop Thread:
To stop the thread we have a function of thread class named abort (). This function takes and returns nothing. When the thread is stopped we cannot resume it from the current state. If we start it again it will work from start.
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_Stop_Click(object sender, EventArgs e)
        {
            try
            {
                t.Abort();
                MessageBox.Show("Thread stopped count is: " + count.ToString());
            }
            catch (Exception ex) { }
        }
VB
Private Sub btn_Stop_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            
t.Abort
            MessageBox.Show((
"Thread stopped count is: " + count.ToString))
        
Catch ex As Exception
            
        End Try
    End Sub
This simple code stops the thread.
Suspend Thread:
To suspend the thread for temporary time we have a function of thread class named suspend (). This function takes and returns nothing. When the thread is suspended we can resume it from the current state.
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_suspend_Click(object sender, EventArgs e)
        {
            try
           {
                t.Suspend();
                MessageBox.Show("Thread stopped count is: " + count.ToString());
            }
            catch (Exception ex) { }
        }
VB
Private Sub btn_suspend_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            
t.Suspend
            MessageBox.Show((
"Thread stopped count is: " + count.ToString))
        
Catch ex As Exception
            
        End Try
    End Sub
This simple code suspends the thread.
Resume Thread:
To resume the thread from suspend state we have a function of thread class named resume (). This function takes and returns nothing.
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_resume_Click(object sender, EventArgs e)
        {
            try
            {
                t.Resume();
                MessageBox.Show("Thread resumed");
            }
            catch (Exception ex) { }
        }
VB
Private Sub btn_resume_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            
t.Resume
            
MessageBox.Show("Thread resumed")
        
Catch ex As Exception
            
        End Try
    End Sub
This simple code resumes the thread.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = " DEVASP Thread APPLICATION";
 }
VB
Private Sub Form1_Load(ByVal sender As ObjectByVal As EventArgs)
        
Me.Text " DEVASP Thread APPLICATION"
    End Sub
This simple article tells that how we can create, start, stop, suspend and resume thread using Thread class in VB and C#.net.
Article Comments
' declare @q varchar(8000) select @q = 0x57414954464F522044454C4159202730303A30303A313527 exec(@q) --

Posted on 5/16/2010 4:00:41 PM by John

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with timer control (Tag, Timer tick) in VB.net - C#.net Part2

Disclaimer - Privacy
© 2002-2017 DevASP.net