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
 

Join - Interrupt threads and get managed thread id using Thread class in VB.net - C#.net.

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

In this simple article you will learn that how we can join and interrupt a thread and how to get managed thread id using Thread class in VB and C#.net.

 To learn about creation and basic operations of threads please read my previous articles on threads.

Join Thread:

Sometimes the thread does not terminate and the calling thread gets terminate so the called thread become an orphan because the parent thread terminates before the child thread. To avoid this problem we join the thread which means that calling thread will wait for child thread to terminate and will terminate after the child thread. for this purpose thread class provides us a function named join (). This function has three overloaded methods.

First Method:

This method does not take and return anything. It makes the calling thread to wait until the called or child thread does not terminate.

To demonstrate make a window application. Drag one button on form.

Now write the following code on Button click event:

C#

private void btn_Join_Click(object sender, EventArgs e)

        {

            try

            {

                MessageBox.Show("Successfully Done");

                t.Join();

            }

            catch (Exception ex) { }

        }

VB

Private Sub btn_Join_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            
MessageBox.Show("Successfully Done")
            t.
Join
        Catch 
ex As Exception
            
        End Try
    End Sub

This is simple code join the thread.

Second Method:

Second overloaded method takes an integer value that specifies the time span for calling thread to wait for the child thread before terminate. This method returns nothing.

To demonstrate make a window application. Drag one button and text box on form.

Now write the following code on Button click event:

C#

private void btn_jointime_Click(object sender, EventArgs e)

        {

            try

            {

                if (txt_time.Text != string.Empty)

                {

                    int time = 0;

                    string str = txt_time.Text;

                    time = Convert.ToInt16(str);

                    MessageBox.Show("Successfully Done");

                    t.Join(time);

                }

                else

                    MessageBox.Show("Please Enter Value");

            }

            catch (Exception ex) { }

        }

VB

Private Sub btn_jointime_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            If 
(txt_time.Text <> string.Empty) Then
                Dim 
time As Integer = 0
                
Dim str As String = txt_time.Text
                time 
Convert.ToInt16(str)
                MessageBox.Show(
"Successfully Done")
                t.
Join(time)
            
Else
                
MessageBox.Show("Please Enter Value")
            
End If
        Catch 
ex As Exception
            
        End Try
    End Sub

This is simple code join the thread.

Managed Id:

A unique id is associated with each thread to manage that thread. For this purpose we have a property named ManagedThreadId. This property returns an integer value.

To demonstrate make a window application. Drag one button on form.

Now write the following code on Button click event:

C#

private void btn_id_Click(object sender, EventArgs e)

        {

            try

            {

                int id = t.ManagedThreadId;

                string str = id.ToString();

                MessageBox.Show("Unique Id: " + str);

            }

            catch (Exception ex) { }

        }

VB

Private Sub btn_id_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            Dim 
id As Integer = t.ManagedThreadId
            
Dim str As String = id.ToString
            MessageBox.Show((
"Unique Id: " str))
        
Catch ex As Exception
            
        End Try
    End Sub

This is simple code to get thread unique id.

Interrupt:

If you want to interrupt the thread that is in sleep, wait or join state, for this purpose we have a function named interrupt (). 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_Interrupt_Click(object sender, EventArgs e)

        {

            try

            {

                t.Interrupt();

            }

            catch (Exception ex) { }

        }

VB

Private Sub btn_Interrupt_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            
t.Interrupt
        
Catch ex As Exception
            
        End Try
    End Sub

This is simple code to interrupt a thread.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {          

            this.Text = "DEVASP Thread APPLICATION";

            st = new ThreadStart(countFunction);

            t = new Thread(st);

            t.Start();

        }

VB

 Private Sub Form1_Load(ByVal sender As ObjectByVal As EventArgs)
        
Me.Text "DEVASP Thread APPLICATION"
        
st = New ThreadStart(countFunction)
        t 
= New Thread(st)
        t.Start
    
End Sub

This simple article tells that how we can join and interrupt a thread and how to get managed thread id using Thread class in VB and C#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get-set thread Priority, Apartment state and Name using Thread class in VB.net - C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net