Tuesday, June 30, 2009

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.


See full detail: http://www.codeproject.com/KB/cs/UsingEventForLongProcess.aspx

No comments: