D
Dan
I'm new to multithreading and I'd like to ask a generic question about
application structure. The scenario is:
- a winforms c# application serves as UI to expose the functions of an
'engine' class to the user. The engine class is completely self-contained
and is a data member of the UI.
- when the UI launches a lengthy process executed from the engine, it should
update various controls (e.g. a listview with some logging) to reflect the
progress of the operations; further, the user must be able to abort the
process at any moment.
The only purpose of multithreading here is to keep the UI responsive and
updated during a lengthy process. Here's how I implement this:
- fields of UI class (a Form-derived class) are the engine itself (let's
call it _engine) plus a Thread (say _tEngine), a progress window form and a
flag named _bWorking.
- when the user launches the lengthy operation, this flag is set to true and
the progress form is created and shown (modeless); I then create a delegate
for the thread and the thread itself, e.g.
ThreadStart worker = new ThreadStart(_eng.DoSomeWork);
_tEngine = new Thread(worker);
_tEngine.Start();
Then this function exits.
- the engine fires some events to notify the client about its progress; the
UI has subscribed to these events so that when handling them it can display
some data into its controls (data are passed back as event arguments, where
any reference object is a clone of the original).
- when the engine has finished, it fires an 'all-done' event which is
handled by the UI to reset the _bWorking flag, close the progress window and
destroy it.
- if the user cancels the process by closing the progress window, the window
fires a Cancel event which is handled by the UI class which calls
_tEngine.Abort(), resets the working flag and destroys the progress window.
I've created a dummy application do test this scenario and it seems to work
fine: the engine keeps working while the UI is responsive and is updated
continuously, until it ends or the user cancels. Anyway, before implementing
it in my real-word application I'd like to know if this is ok or I'm doing
something wrong which could be disastrous...
Thanks to all!
application structure. The scenario is:
- a winforms c# application serves as UI to expose the functions of an
'engine' class to the user. The engine class is completely self-contained
and is a data member of the UI.
- when the UI launches a lengthy process executed from the engine, it should
update various controls (e.g. a listview with some logging) to reflect the
progress of the operations; further, the user must be able to abort the
process at any moment.
The only purpose of multithreading here is to keep the UI responsive and
updated during a lengthy process. Here's how I implement this:
- fields of UI class (a Form-derived class) are the engine itself (let's
call it _engine) plus a Thread (say _tEngine), a progress window form and a
flag named _bWorking.
- when the user launches the lengthy operation, this flag is set to true and
the progress form is created and shown (modeless); I then create a delegate
for the thread and the thread itself, e.g.
ThreadStart worker = new ThreadStart(_eng.DoSomeWork);
_tEngine = new Thread(worker);
_tEngine.Start();
Then this function exits.
- the engine fires some events to notify the client about its progress; the
UI has subscribed to these events so that when handling them it can display
some data into its controls (data are passed back as event arguments, where
any reference object is a clone of the original).
- when the engine has finished, it fires an 'all-done' event which is
handled by the UI to reset the _bWorking flag, close the progress window and
destroy it.
- if the user cancels the process by closing the progress window, the window
fires a Cancel event which is handled by the UI class which calls
_tEngine.Abort(), resets the working flag and destroys the progress window.
I've created a dummy application do test this scenario and it seems to work
fine: the engine keeps working while the UI is responsive and is updated
continuously, until it ends or the user cancels. Anyway, before implementing
it in my real-word application I'd like to know if this is ok or I'm doing
something wrong which could be disastrous...
Thanks to all!