Making UI more responsive using Threading


Description:

This example shows how threads can help us to make User interface more responsive when we have some background jobs. It has got pairs of buttons (cmdUnresponsiveStart, cmdToggleForUnResponsiveStart) and (cmdresponsive,  cmdToggleForResponsive).

There is boolean runflag which checks if counter needs to be incremented and set the counter value in txtcounter.when we click on toggle buttons the counter is stopped,because go checks for this flag.

  1. Clicking on cmdUnresponsiveStart would make user interface unresponsive since it calls go which is in while loop. So if some one tries to click on cmdToggleForUnResponsiveStart the UI would not be responsive.

  2. Clicking on cmdToggleForResponsive would start a new thread and call go. So now if some one tries to click on cmdToggleForResponsive the user interface responds and runflag is set to false and the counter is stopped.

What is needed to compile?
.NET SDK

How to Compile?
csc /r:System.dll /r:System.winforms.dll /r:Microsoft.win32
.interop.dll/r:System.Drawing.dll responsiveui.cs

Code:

namespace UnresponsiveUI
{
using System;
using System.Threading;
//using System.Drawing;
//using System.Collections;
//using System.ComponentModel;
using System.WinForms;
// using System.Data;
/// <summary>
/// Summary description for frmTest.
/// </summary>
public class frmTest : System.WinForms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components;
private System.WinForms.Button cmdToggleForUnResponsiveStart;
private System.WinForms.Button cmdresponsive;
private System.WinForms.Button cmdToggleForResponsive;
private System.WinForms.Button cmdUnresponsiveStart;
private System.WinForms.TextBox txtCounter;
private System.Int32 counter;
public System.Boolean runFlag=false;
private System.Threading.Thread oThread;
public frmTest()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
public override void Dispose()
{
base.Dispose();
components.Dispose();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container ();
this.cmdToggleForResponsive = new System.WinForms.Button ();
this.cmdToggleForUnResponsiveStart = new System.WinForms.Button ();
this.cmdresponsive = new System.WinForms.Button ();
this.txtCounter = new System.WinForms.TextBox ();
this.cmdUnresponsiveStart = new System.WinForms.Button ();
//@this.TrayHeight = 0;
//@this.TrayLargeIcon = false;
//@this.TrayAutoArrange = true;
cmdToggleForResponsive.Location = new System.Drawing.Point (128, 112);
cmdToggleForResponsive.Size =
new System.Drawing.Size (168, 24);
cmdToggleForResponsive.TabIndex = 4;
cmdToggleForResponsive.Text = "ToggleForResponsiveStart";
cmdToggleForResponsive.Click +=
new System.EventHandler (this.cmdToggleForResponsive_Click);
cmdToggleForUnResponsiveStart.Location =
new System.Drawing.Point (127, 80);
cmdToggleForUnResponsiveStart.Size =
new System.Drawing.Size (168, 24);
cmdToggleForUnResponsiveStart.TabIndex = 2;
cmdToggleForUnResponsiveStart.Text = "ToggleForUnResponsiveStart";
cmdToggleForUnResponsiveStart.Click +=
new System.EventHandler (this.cmdToggleForUnResponsiveStart_Click);
cmdresponsive.Location =
new System.Drawing.Point (8, 112);
cmdresponsive.Size =
new System.Drawing.Size (112, 24);
cmdresponsive.TabIndex = 3;
cmdresponsive.Text = "ResponsiveStart";
cmdresponsive.Click +=
new System.EventHandler (this.cmdresponsive_Click);
txtCounter.Location =
new System.Drawing.Point (80, 40);
txtCounter.TabIndex = 0;
txtCounter.Size =
new System.Drawing.Size (104, 20);
cmdUnresponsiveStart.Location =
new System.Drawing.Point (7, 80);
cmdUnresponsiveStart.Size =
new System.Drawing.Size (112, 24);
cmdUnresponsiveStart.TabIndex = 1;
cmdUnresponsiveStart.Text = "UnresponsiveStart";
cmdUnresponsiveStart.Click +=
new System.EventHandler (this.cmdUnresponsiveStart_Click);
this.Text = "UnResponsiveUI";
this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
this.ClientSize = new System.Drawing.Size (296, 273);
this.Click += new System.EventHandler (this.frmTest_Click);
this.Controls.Add (this.cmdToggleForResponsive);
this.Controls.Add (this.cmdresponsive);
this.Controls.Add (this.cmdToggleForUnResponsiveStart);
this.Controls.Add (this.cmdUnresponsiveStart);
this.Controls.Add (this.txtCounter);
}
protected void frmTest_Click (object sender, System.EventArgs e)
{
}
public void go()
{
while(true)
{
try
{
//Thread.Sleep(100);
if (runFlag)
{
//counter=counter+1;
txtCounter.Text=Int32.ToString(counter++);
}
}
catch(Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
}
protected void cmdToggleForResponsive_Click (object sender, System.EventArgs e)
{
if (runFlag)
{
oThread.Abort();
}
runFlag=!runFlag;
}
protected void cmdresponsive_Click (object sender, System.EventArgs e)
{
if (runFlag==false)
{
oThread =
new Thread(new ThreadStart(this.go));
runFlag=
true;
oThread.Start();
}
}
protected void cmdToggleForUnResponsiveStart_Click (object sender, System.EventArgs e)
{
runFlag=!runFlag;
}
protected void cmdUnresponsiveStart_Click (object sender, System.EventArgs e)
{
runFlag=
true;
go();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
public static void Main(string[] args)
{
Application.Run(
new frmTest());
}
}
}

Up Next
    Ebook Download
    View all
    Learn
    View all