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
 

Get-set thread Priority, Apartment state and Name using Thread class in VB.net - C#.net.

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

In this simple article you will learn that how we can get or set thread priority, apartment state and name using Thread class in VB and C#.net.

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

Thread Priority:

Priority means the time slice given to thread on cpu. We can adjust its time slice according to our requirement. To do this we have a property of Thread class named priority. This property takes and returns value of enumeration named ThreadPriority.

SET:

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

Now write the following code on Button click event:

C#

private void btn_priority_Click(object sender, EventArgs e)

        {

            if (rb_Normal.Checked)

            {

                t.Priority = ThreadPriority.Normal;

            }

            else

                if (rb_low.Checked)

                {

                    t.Priority = ThreadPriority.Lowest;

                }

                else

                    if (rb_High.Checked)

                    {

                        t.Priority = ThreadPriority.Highest;

                    }

                    else

                        if (rb_bnormal.Checked)

                        {

                            t.Priority = ThreadPriority.BelowNormal;

                        }

                        else

                        {

                            t.Priority = ThreadPriority.AboveNormal;                       

                        }

            MessageBox.Show("SUCCESSFULLY DONE");

        }

VB

Private Sub btn_priority_Click(ByVal sender As ObjectByVal As EventArgs)
        
If rb_Normal.Checked Then
            
t.Priority ThreadPriority.Normal
        
ElseIf rb_low.Checked Then
            
t.Priority ThreadPriority.Lowest
        
ElseIf rb_High.Checked Then
            
t.Priority ThreadPriority.Highest
        
ElseIf rb_bnormal.Checked Then
            
t.Priority ThreadPriority.BelowNormal
        
Else
            
t.Priority ThreadPriority.AboveNormal
        
End If
        
MessageBox.Show("SUCCESSFULLY DONE")
    
End Sub

This is simple code to set the thread priority.

GET:

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

Now write the following code on Button click event:

C#

private void btn_getPriority_Click(object sender, EventArgs e)

        {

            try

            {

                ThreadPriority TP = t.Priority;

                MessageBox.Show(TP.ToString());

            }

            catch (Exception ex) { }

        }

VB

Private Sub btn_getPriority_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            Dim 
TP As ThreadPriority t.Priority
            MessageBox.Show(TP.ToString)
        
Catch ex As Exception
            
        End Try
    End Sub

This is simple code to get the thread priority.

Thread Name:

If you want to change or get the name of thread for this purpose we have a property of Thread class named name. This property takes and returns string value.

SET:

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_Name_Click(object sender, EventArgs e)

        {

            try

            {

                if (txt_name.Text != string.Empty)

                {

                    string name = txt_name.Text;

                    t.Name = name;

                    MessageBox.Show("Done");

                }

                else

                    MessageBox.Show("Please enter Name");

            }

            catch (Exception ec) { }

        }

VB

Private Sub btn_Name_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            If 
(txt_name.Text <> string.Empty) Then
                Dim 
name As String = txt_name.Text
                t.Name 
name
                MessageBox.Show(
"Done")
            
Else
                
MessageBox.Show("Please enter Name")
            
End If
        Catch 
ec As Exception
            
        End Try
    End Sub

This is simple code to set the thread name.

GET:

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

Now write the following code on Button click event:

C#

private void btn_getname_Click(object sender, EventArgs e)

        {

             try

             {

                 string name = t.Name;

                 MessageBox.Show("Name :" + name);

             }

             catch (Exception ec) { }

        }

VB

Private Sub btn_getname_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            Dim 
name As String = t.Name
            MessageBox.Show((
"Name :" + name))
        
Catch ec As Exception
            
        End Try
    End Sub

This is simple code to get the thread name.

Thread Apartment State:

Apartment state is state of thread which tells that in what kind of apartment thread will enter. It can be in multithreaded or single threaded or unknown.

Set:

If you want to set the apartment state of thread, for this purpose we have a function of Thread class named SetApartmentState(). This function takes value of Apartment state enumeration.

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

Now write the following code on Button click event:

C#

private void btn_Apartement_Click(object sender, EventArgs e)

        {

            if(rb_mta.Checked)

            {

                t.SetApartmentState(ApartmentState.MTA);

            }

            else

                if (rb_single.Checked)

                {

                    t.SetApartmentState(ApartmentState.STA);

                }

                else

                    t.SetApartmentState(ApartmentState.Unknown);

            MessageBox.Show("SUCCESSFULLY DONE");

        }

VB

Private Sub btn_Apartement_Click(ByVal sender As ObjectByVal As EventArgs)
        
If rb_mta.Checked Then
            
t.SetApartmentState(ApartmentState.MTA)
        
ElseIf rb_single.Checked Then
            
t.SetApartmentState(ApartmentState.STA)
        
Else
            
t.SetApartmentState(ApartmentState.Unknown)
        
End If
        
MessageBox.Show("SUCCESSFULLY DONE")
    
End Sub

This is simple code to set the thread apartment state.

GET:

If you want to get the apartment state of thread, for this purpose we have a function of Thread class named GetApartmentState(). This function returns value of Apartment state enumeration.

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

Now write the following code on Button click event:

C#

private void btn_getApartement_Click(object sender, EventArgs e)

        {

            try

            {

                ApartmentState AP = t.GetApartmentState();

                MessageBox.Show(AP.ToString());

            }

            catch (Exception ex) { }

        }

VB

Private Sub btn_getApartement_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            Dim 
AP As ApartmentState t.GetApartmentState
            MessageBox.Show(AP.ToString)
        
Catch ex As Exception
            
        End Try
    End Sub

This is simple code to get the thread apartment state.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

            rb_Normal.Checked = true;

            rb_single.Checked = true;

            this.Text = "DEVASP Thread APPLICATION";

            st = new ThreadStart(countFunction);

            t = new Thread(st);   

   }

VB

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

This simple article tells that how we can get or set thread priority, apartment state and name using Thread class in VB and C#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get encoding, format provider, life time service by using “string writer” class in vb and C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net