473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Multithreading design question

I'm new to writing multithreaded apps and I have a design question. I
have a winforms app and a class which has a method that does processing
which is time intensive. I want the user to be able to kick off the
process and continue to work in the appliaction while getting progress
updates and the ability to cancel. The method that seems easiest to me
is this:

The class exposes certain events for progress. Start, ProgressUpdate,
and End. Create an EventArgs class for passing data to the handlers.
The form has an instance of this class with handlers for these events.
When the user clicks go, a new thread kicks off that calls the method
on the class to do processing.
The method in the class raises the events at the appropriate time. The
ui thread traps those events and updates the UI accordingly. The
processing method in the class checks a variable after each chunk of
processing to see if the user wants to cancel and raises the
ProgressUpdate event. If the user clicks cancel in the form, the
cancel member of the class instance is set to indicate the user's
request and execution ends after the latest chunck of processing is
complete.

Since I'm new to multithreading, I'm unsure if this is a decent way to
make everything work. Is this a solid design or am I commiting
horrible crimes against the multithreading gods?

Paul

Jul 21 '05 #1
1 2008
Paul,

That is an acceptable approach. However, you need to be aware of some
of the pitfalls inherent in multithreading programming especially when
Windows Forms are involved.

Threading in Windows Forms:
<http://www.yoda.arachsys.com/csharp/threads/winforms.shtml>

Shutting Down Worker Threads Gracefully
<http://www.yoda.arachsys.com/csharp/threads/shutdown.shtml>

Notice Jon's use of the ISynchronizeInvoke.BeginInvoke method in his
article "Threading in Windows Forms". That's the most important part.
Now, since you have a separate class that handles the long processing I
suggest you design it similar to the way Microsoft designed the
System.Timers.Timer class. That timer exposes a SynchronizingObject
property that takes an ISynchronizeInvoke which it uses to
automatically marshal its events on the thread hosting the
synchronizing object (which is usually the UI thread). For example:

public class IntensiveComputation
{
private ISynchronizeInvoke _Synchronizer = null;

public ISynchronizeInvoke SynchronizingObject
{
get { return _Synchronizer; }
set { _Synchronizer = value; }
}

public void Start()
{
// Start a thread here to run LongRunningMethod.
}

private void LongRunningMethod()
{
while (!done)
{
RaiseProgressUpdate(this, new EventArgs());
}
}

private void RaiseProgressUpdate(object sender, EventArgs args)
{
if (_Synchronizer != null && _Synchronizer.InvokeRequired)
{
// This recalls the current method, but on the desired thread.
Delegate m = new EventHandler(this.RaiseProgressUpdate);
object[] a = new object[] { sender, args };
_Synchronizer.BeginInvoke(m, a);
}
else
{
// Now we can actually raise the event.
if (ProgressUpdate != null)
{
ProgressUpdate(sender, args);
}
}
}
}

When the SynchronizingObject property is set to a Form or Control the
class automatically marshals ProgressUpdate on the UI thread, otherwise
the event is executed on the worker thread. You can use this pattern
for the Start and End events as well. This is a nice way of
encapsulating the marshaling logic. Be sure to read Jon's other
article on stopping worker threads gracefully.

Brian

di**@usa.net wrote:
I'm new to writing multithreaded apps and I have a design question. I
have a winforms app and a class which has a method that does processing
which is time intensive. I want the user to be able to kick off the
process and continue to work in the appliaction while getting progress
updates and the ability to cancel. The method that seems easiest to me
is this:

The class exposes certain events for progress. Start, ProgressUpdate,
and End. Create an EventArgs class for passing data to the handlers.
The form has an instance of this class with handlers for these events.
When the user clicks go, a new thread kicks off that calls the method
on the class to do processing.
The method in the class raises the events at the appropriate time. The
ui thread traps those events and updates the UI accordingly. The
processing method in the class checks a variable after each chunk of
processing to see if the user wants to cancel and raises the
ProgressUpdate event. If the user clicks cancel in the form, the
cancel member of the class instance is set to indicate the user's
request and execution ends after the latest chunck of processing is
complete.

Since I'm new to multithreading, I'm unsure if this is a decent way to
make everything work. Is this a solid design or am I commiting
horrible crimes against the multithreading gods?

Paul


Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gianguz | last post by:
In the following example Global is able to create and manage access to objects of any kind (even in a multithreading environment) with and index value attached to. So a Global<0, string> is a...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
8
by: Lenn | last post by:
Hello, Just some background: I'm developing an application that basically executes series of tasks. So far I have 2 group of tasks run on different threads (2 different threads). Which run in...
2
by: SStory | last post by:
Here is the situation. I want to display Icons, Type of file etc from a file extension. Upon initial program load I may only need icons for certain files. But other operations will require...
2
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and...
1
by: dixp | last post by:
I'm new to writing multithreaded apps and I have a design question. I have a winforms app and a class which has a method that does processing which is time intensive. I want the user to be able...
2
by: Jeff | last post by:
Hey ..NET 2.0 I'm developing an application which will perform some webservice calls and I believe having those calls in a separate thread may help the app run smoother No user are waiting...
0
by: Keith Thompson | last post by:
jurij <jurij.ivastsuk@waxar.euwrites: Standard C doesn't have threads. Try comp.programming.threads. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.