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

Event Driven

This document defines a Windows Forms application that demonstrates threading in C#. It creates four thread objects with different priority levels and assigns each one a method from the MyThreadClass. The threads are started concurrently and then joined to complete sequentially. The MyThreadClass contains two methods, Thread1 and Thread2, that each loop and write output with the thread name and iteration while sleeping between iterations.

Uploaded by

bebedictmalay
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Event Driven

This document defines a Windows Forms application that demonstrates threading in C#. It creates four thread objects with different priority levels and assigns each one a method from the MyThreadClass. The threads are started concurrently and then joined to complete sequentially. The MyThreadClass contains two methods, Thread1 and Thread2, that each loop and write output with the thread name and iteration while sleeping between iterations.

Uploaded by

bebedictmalay
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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 frmTrackThread
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
Console.WriteLine("-Thread Starts-");
lblThread.Text = "-Thread Starts-";

Thread ThreadA = new Thread(MyThreadClass.Thread1);


Thread ThreadB = new Thread(MyThreadClass.Thread2);
Thread ThreadC = new Thread(MyThreadClass.Thread1);
Thread ThreadD = new Thread(MyThreadClass.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-";
}
class MyThreadClass
{
public static void Thread1()
{
for (int i = 0; i <= 2; i++)
{
Thread thread = Thread.CurrentThread;
Console.WriteLine("Name of thread: " + thread.Name + " = " +
i);
Thread.Sleep(450);
}
}
public static void Thread2()
{
for (int i = 0; i <= 6; i++)
{
Thread thread = Thread.CurrentThread;
Console.WriteLine("Name of thread: " + thread.Name + " = " +
i);
Thread.Sleep(1500);
}
}
}
}

You might also like