You are on page 1of 2

using System;

using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
namespace Threading
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}

private void Count()


{
while (Convert.ToInt32(lblCount.Text) < int.MaxValue)
lblCount.Text = (Convert.ToInt32(lblCount.Text) +
1).ToString();
}
private void ChangeLblCount(int number)
{
lblCount.Text = number.ToString();
}
private void CountFrom(object o)
{
lblCount.Text = o.ToString();
while (Convert.ToInt32(lblCount.Text) < int.MaxValue)
{
int count = Convert.ToInt32(lblCount.Text) + 1;
ChangeLableDelegate changeLblCountdlgt = ChangeLblCount;
this.Invoke(changeLblCountdlgt, count);
Thread.Sleep(100);
}

private void btnStartCount_Click(object sender, EventArgs e)


{
ThreadStart thrdStart = new ThreadStart(Count);
thrdCount = new Thread(thrdStart);
thrdCount.IsBackground = true;
thrdCount.Start();
}

private void btnStartCountFrom_Click(object sender, EventArgs e)


{
ParameterizedThreadStart paramThrdStart = new
ParameterizedThreadStart(CountFrom);
Thread thrdCountFrom = new Thread(paramThrdStart);
thrdCountFrom.Start(txtStartNumber.Text);
lstStartFromThrds.Add(thrdCountFrom);
}

private void btnKill_Click(object sender, EventArgs e)


{
Process.GetCurrentProcess().Kill();
}
private void btnAbortStartCountThrd_Click(object sender, EventArgs e)
{
thrdCount.Abort();
}
private void btnAbortAllCountFromThrd_Click(object sender, EventArgs e)
{
foreach (Thread thrd in lstStartFromThrds)
thrd.Abort();
}
private void Lambda_Click(object sender, EventArgs e)
{
new Thread(() => {
while (Convert.ToInt32(lblCount.Text) < int.MaxValue)
{
int x = Convert.ToInt32(lblCount.Text) + 1;
this.Invoke(new
ChangeLabelDelegateWithoutPrarameter(() =>
{
lblCount.Text = x.ToString();
}));
}
}).Start();
}
delegate void ChangeLableDelegate(int x);
delegate void ChangeLabelDelegateWithoutPrarameter();
Thread thrdCount;
List<Thread> lstStartFromThrds = new List<Thread>();
}
}

Assignments
1. Change the start from thread using Lambda Expression.

You might also like