0% found this document useful (0 votes)
202 views

Laboratory Exercise: Thread Priority: 1. Frmtrackthread Window Form

This document describes a laboratory exercise on thread priority in C#. It defines two thread classes - Thread1 and Thread2 - that run simple for loops. The main form class creates four threads, sets their priorities to different levels (Highest, Normal, AboveNormal, BelowNormal), and starts and joins them. The output shows the threaded processes running at different speeds according to their priority settings.

Uploaded by

Namnam Dela Cruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
202 views

Laboratory Exercise: Thread Priority: 1. Frmtrackthread Window Form

This document describes a laboratory exercise on thread priority in C#. It defines two thread classes - Thread1 and Thread2 - that run simple for loops. The main form class creates four threads, sets their priorities to different levels (Highest, Normal, AboveNormal, BelowNormal), and starts and joins them. The output shows the threaded processes running at different speeds according to their priority settings.

Uploaded by

Namnam Dela Cruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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