Matthew Steven Kelly

Threading in C#

February10

A quick snippet of C# code to process data in a thread, and update a status label on the GUI to show how far along in the process it is.

To use this code you just need a generic Windows Form C# application with a label control named “lblStatus” and a button control named “btnProcessData”. Additionall add a click event to the button:

In Form1.Designer.cs:

this.btnProcessData.Click += new System.EventHandler(this.btnProcessData_Click);

Form1.cs

using System;
using System.Threading;
using System.Windows.Forms;
using System.IO;
namespace ProcessData
{
  public partial class Form1 : Form
  {
    private System.Threading.Thread _thread;
    public Form1()
    {
        InitializeComponent();
    }
    public delegate void UpdateLabelTextCallback(Label box, string msg);
    /// <summary>
    /// Allows threads to update text on a label
    /// </summary>
    /// <param name="lbl"></param>
    /// <param name="msg"></param>
    private void UpdateLabelText(Label lbl, string msg)
    {
        lbl.Text = msg;
    }
    /// <summary>
    /// Function that processes data
    /// </summary>
    private void btnProcessData_Click(object sender, EventArgs e)
    {
        lblStatus.Invoke(new UpdateLabelTextCallback(this.UpdateLabelText), new object[] { lblStatus, string.Format("Starting to process…") });
        ThreadStart TS;
        try
        {
            TS = new ThreadStart(ProcessData);
            _thread = new Thread(TS);
            _thread.Start();
        }
        catch (Exception E)
        {
            MessageBox.Show(string.Format("{0} {1} {2}", E.Message.ToString(), E.Source.ToString(), E.StackTrace.ToString()));
            _thread.Abort();
        }
    }
    /// <summary>
    /// Function that processes data
    /// </summary>
    private void ProcessData()
    {
        int iMax = 10;
        for (int i=1; i<=iMax; i++)
        {
            Thread.Sleep(500);
            lblStatus.Invoke(new UpdateLabelTextCallback(this.UpdateLabelText), new object[] { lblStatus, string.Format("Processing {0} or {1}", i, iMax) });
        }
        lblStatus.Invoke(new UpdateLabelTextCallback(this.UpdateLabelText), new object[] { lblStatus, string.Format("Finished processing…") });
    }
  }
}
posted under Random | No Comments »

Programmatically click a C# button

February10

Many times I am writing a c# application where a certain function needs to be performed on a timer event as well as from a user clicking a button. So in the effort of keeping the programmatic functionality the same as the user clicked event, we can programmatically click the button:

btnProcessData_Click(this, new EventArgs());

One could argue that it would be just as easy to to call a function from both locations such as ProcessData() instead of clicking the button programmatically, but the advantage is if a programmer in the future (or even you down the road) adds logic to the _Click event, it won’t be implemented in the triggered event (an event you or the new programmer may not remember or know occurs).

posted under Random | No Comments »

C Sharp Master

November22

This weekend I became a Brain Bench certified “C# Master”. Score: 4.33. I scored higher than 96% of all previous test takers.

View results: http://www.brainbench.com/transcript.jsp?pid=7948365

posted under My Career | No Comments »