You are on page 1of 4

Laboratory Exercise:

Thread Priority

1. FrmTrackThread window Form:

2. MyThreadClass Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Thread_Priority
{
internal class MyThreadClass
{
public static void Thread1()
{

for (int t1 = 0; t1 <= 2; t1++)


{
Thread thread = Thread.CurrentThread;
Console.WriteLine("Name of thread: " + thread.Name + " = " +
t1);
Thread.Sleep(500);
}

}
public static void Thread2()
{
for (int t2 = 0; t2 <= 6; t2++)
{
Thread thread = Thread.CurrentThread;
Console.WriteLine("Name of thread: " + thread.Name + " = " +
t2);
Thread.Sleep(1500);
}

}
}
}

3. FrmTrackThread Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Thread_Priority
{
public partial class frmTrackThread : Form
{
public frmTrackThread()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
Console.WriteLine("-Thread Starts-");
lblThread.Text = "-Thread Starts-";
ThreadStart thread1 = new ThreadStart(MyThreadClass.Thread1);
ThreadStart thread2 = new ThreadStart(MyThreadClass.Thread2);
Thread ThreadA = new Thread(thread1);
Thread ThreadB = new Thread(thread1);
Thread ThreadC = new Thread(thread2);
Thread ThreadD = new Thread(thread2);

ThreadA.Name = "Thread A Process ";


ThreadB.Name = "Thread B Process ";
ThreadC.Name = "Thread C Process ";
ThreadD.Name = "Thread D Process ";

ThreadA.Priority = ThreadPriority.Highest;
ThreadB.Priority = ThreadPriority.Normal;
ThreadC.Priority = ThreadPriority.AboveNormal;
ThreadD.Priority = ThreadPriority.BelowNormal;

ThreadA.Start();
ThreadB.Start();
ThreadC.Start();
ThreadD.Start();

ThreadA.Join();
ThreadB.Join();
ThreadC.Join();
ThreadD.Join();

Console.WriteLine("-End of Thread-");

lblThread.Text = "-End of Thread-";


}
}
}

4. Output of the program:

Thread Start:
End of the Thread:

You might also like