Click here to Skip to main content
Click here to Skip to main content

Using Events and Threading to Communicate Long Processes to UI

, , 12 Dec 2008
Rate this:
Please Sign up or sign in to vote.
Using events and threading to communicate long processes to UI
UI_Illustration_LongProcess.JPG

Introduction

Sometimes it is necessary to communicate or “send” information from a long data process to User Interface (UI) in order to let the user know how it is going.

Due to the fact that it is recommended to separate logic or database work from UI, this situation becomes complicated to solve without mix (or mess?) layers.

Here it is important to use events inside the big and long process in order to let the UI know the progress of the actions, without mixing layers.

Background

It’s better to be familiarized with threads, delegates and events before to implement this suggestion to your projects.

Using the Code

The Visual Studio solution in this example uses two projects, one to simulate databases work (or any other long process) and a different project for UI with two forms. The first one is for simulating the application itself and the second one for process informing. Both projects are in different assemblies, the long process is a DLL with different namespace from UI that is the main EXE.

In the long process, probably inside a for or a foreach, add the instruction to rise the event, "sending" to UI the information that will be necessary to notify the user about any progress.

public void BigProcess()
{
    float x = 0;
    float Limit = 150;

    for (int i = 0; i <= Limit; i++)
    {
        x = (i / Limit) * 100;
        //Rise the event
        ProgressStatus(new ProgressEventArgs(Convert.ToInt32(x), i.ToString(), x));
        Thread.Sleep(100); //Simulating some processes here
    }
}

Then in the UI, it is necessary to instantiate the form that will be used to inform the user about the progress of the process. But, for better performance, it is necessary to “split” the work using threads.

UI1.JPG

private void button1_Click(object sender, EventArgs e)
{
    //Use other thread for DB work  
    ThreadPool.QueueUserWorkItem(new WaitCallback(DBWork));
    //show the progress form
    frmProgr.ShowDialog();
}

private void DBWork(object o)
{
    DBprocess dst = new DBprocess();
            
    //Subscribe to event
    dst.ProgressStatus += new DBprocess.ProgressEventHandler(UpdateProgressBar);  
    dst.BigProcess();
}

void UpdateProgressBar(ProgressEventArgs e)
{
    //At this point, it is possible to check if frmProgr needs
    //InvokeRequired, but if the user needs to call many
    //times the big process, then always use Invoke
    SetValuesCallBack values = new SetValuesCallBack(SetValues);
    this.Invoke(values, new object[] { e });
}

//This delegate is for thread-safe call
delegate void SetValuesCallBack(ProgressEventArgs e);
//Here is the thread-safe work done.
private void SetValues(ProgressEventArgs e)
{
    this.frmProgr.progressBar1.Value = e.Percent;
    this.frmProgr.label1.Text = e.Message + " iterations = " + 
        e.Extradata.ToString("###.00") +"%";
    if (e.Extradata == 100.0) //Is the process finished?
    {
        this.frmProgr.progressBar1.Maximum = 100;
        this.frmProgr.progressBar1.Value = 100;
        this.frmProgr.label1.Text = "Big Process Terminated";
        MessageBox.Show("Work done");
        frmProgr.Close();
    }
}

In this case, we are using a different thread for the long process and using thread-safe calls for updating the progress form.

UI_progress.JPG

Then, you have a pretty simple and safe solution to the problem.

Conclusion

As a result, you have a clean application, with specific layers separation and not only a program with long processes running where the final user does not know if it is blocked or doing something strange and obscure inside the "box".

History

  • 12th December, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Authors

Eduardo Lavin
Software Developer (Senior) La Torre del Vigía, A.R.
Mexico Mexico
Computers Systems Engineer

ignotus confutatis
Software Developer
Mexico Mexico
A humble Linux User

Comments and Discussions

 
GeneralGood example of threads and delegates PinmemberDonsw13-Feb-09 11:17 
GeneralGood but I prefer Synchronizationcontext Pinmemberchrono_dev9-Jan-09 2:19 
GeneralSimpler ways PinmemberGonzalo Cao15-Dec-08 21:57 
GeneralMy vote of 2 Pinmembergxdata15-Dec-08 17:17 
GeneralBackgroundWorker PinmemberKevinAG15-Dec-08 13:56 
RantWell written, but certainly not advanced material PinmemberSaar Yahalom12-Dec-08 9:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140718.1 | Last Updated 12 Dec 2008
Article Copyright 2008 by Eduardo Lavin, ignotus confutatis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid