473,396 Members | 2,013 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,396 software developers and data experts.

application and progress dialog in new thread - no worker thread

Hi, I have written a program that takes on some operations much more
time than I expected. As I have seen users clicking wildly on the
screen to make something happen, I want to follow the microsoft
principles: always show them something ;)

First I made up a little status dialog containing a gif image to show
a little animation and a TextBox for a changing status message during
the work of the main program.
Just showing the dialog box (Form) at the start of the lengthy
operation does not work, as the dialog box is of course frozen because
it cannot work while the main message loop is stalled at the call of
the first method. Most solutions work with a non UI worker thread
which signals its status of the work progress to the UI thread. As my
main program is a UI program and all classes in there interact very
close it would be difficult for me to seperate a non UI thread for the
work. My idea was now to create a thread which shows the dialog and I
signal a new status message to the thread from my main UI thread
during the work, while disabling all inputs to the main window and
showing the status dialog topmost.

How would I do something like that?

I guess I just start the dialog by opening the dialog form with
Application.Run(new Statusdialog()) in the worker function of my
thread. But how do I access the dialog to signal new messages to it?
There is only one status dialog shown at a time so there is only one
instance of the dialog. I could create a static instance variable with
an access function in the dialog class to access the instance
following the singleton pattern. But from my experience with C++ this
is an unsafe way and I should use messages for the communication or
does C# have a synchronized keyword like Java to make such calls
thread safe?

Thanks for any help or suggestions,
Alexander
Nov 16 '05 #1
4 11609
Alexander,

It would seem that you would have to do just as much work to create
another dialog in another thread, and marshal all the calls to that dialog
as you would if you separated the UI from the business logic from your
current implementation.

You might have a simpler option. While I don't advocate this, you could
call the static DoEvents method on the Application class, so that queued up
windows messages are processed, therefore making your UI more responsive.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alexander" <jo*******@gmx.de> wrote in message
news:84*************************@posting.google.co m...
Hi, I have written a program that takes on some operations much more
time than I expected. As I have seen users clicking wildly on the
screen to make something happen, I want to follow the microsoft
principles: always show them something ;)

First I made up a little status dialog containing a gif image to show
a little animation and a TextBox for a changing status message during
the work of the main program.
Just showing the dialog box (Form) at the start of the lengthy
operation does not work, as the dialog box is of course frozen because
it cannot work while the main message loop is stalled at the call of
the first method. Most solutions work with a non UI worker thread
which signals its status of the work progress to the UI thread. As my
main program is a UI program and all classes in there interact very
close it would be difficult for me to seperate a non UI thread for the
work. My idea was now to create a thread which shows the dialog and I
signal a new status message to the thread from my main UI thread
during the work, while disabling all inputs to the main window and
showing the status dialog topmost.

How would I do something like that?

I guess I just start the dialog by opening the dialog form with
Application.Run(new Statusdialog()) in the worker function of my
thread. But how do I access the dialog to signal new messages to it?
There is only one status dialog shown at a time so there is only one
instance of the dialog. I could create a static instance variable with
an access function in the dialog class to access the instance
following the singleton pattern. But from my experience with C++ this
is an unsafe way and I should use messages for the communication or
does C# have a synchronized keyword like Java to make such calls
thread safe?

Thanks for any help or suggestions,
Alexander

Nov 16 '05 #2
Hi,

Most solutions work with a non UI worker thread
which signals its status of the work progress to the UI thread. As my
main program is a UI program and all classes in there interact very
close it would be difficult for me to seperate a non UI thread for the
work.


As in all win app. What is the nature of the operations you will perform?
I definely would go for the worker thread if possible. You can perform the
operations in that thread and notify the UI when done, then you can display
the results.

Let me know if you need some code for this.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 16 '05 #3
JJ
Alexander,

you have to do something else:
Statusdialog dialog1 = new Statusdialog(); // Define this outside any
function because you will need it as a global variable.

to show the dialog:
dialog1.Show();

now you can change all properties of the dialog:
dialog1.Text = "Some text";

Now add the activate event to the main form, and add to the function for
that eventhandler:
dialog.Focus();

Vincent

"Alexander" <jo*******@gmx.de> schreef in bericht
news:84*************************@posting.google.co m...
Hi, I have written a program that takes on some operations much more
time than I expected. As I have seen users clicking wildly on the
screen to make something happen, I want to follow the microsoft
principles: always show them something ;)

First I made up a little status dialog containing a gif image to show
a little animation and a TextBox for a changing status message during
the work of the main program.
Just showing the dialog box (Form) at the start of the lengthy
operation does not work, as the dialog box is of course frozen because
it cannot work while the main message loop is stalled at the call of
the first method. Most solutions work with a non UI worker thread
which signals its status of the work progress to the UI thread. As my
main program is a UI program and all classes in there interact very
close it would be difficult for me to seperate a non UI thread for the
work. My idea was now to create a thread which shows the dialog and I
signal a new status message to the thread from my main UI thread
during the work, while disabling all inputs to the main window and
showing the status dialog topmost.

How would I do something like that?

I guess I just start the dialog by opening the dialog form with
Application.Run(new Statusdialog()) in the worker function of my
thread. But how do I access the dialog to signal new messages to it?
There is only one status dialog shown at a time so there is only one
instance of the dialog. I could create a static instance variable with
an access function in the dialog class to access the instance
following the singleton pattern. But from my experience with C++ this
is an unsafe way and I should use messages for the communication or
does C# have a synchronized keyword like Java to make such calls
thread safe?

Thanks for any help or suggestions,
Alexander

Nov 16 '05 #4
I don't like it if people just take the answers and don't tell anybody
if it worked or not - so here is what I did.
I guess I followed the clean solution suggested by Nicholas. I had
only to cover two methods for closing the dialog and for passing
messages to it, so it was not too much nasty work ;)
The only thing I discovered with working with the dialog is that if
you are too fast in invoking functions on it, an exception is raised
because the dialog is not fully created yet, if at all. So as an
addition I passed an event flag to it, that is signaled if the dialog
is ready. I wait for that event on starting the tread (maybe I will
add a timeout here, because I don't want the application to stall just
because a little status box isn't opening).

So here is in a short version what I did:

----------------------------------------------------------------------------
using System;
using System.Windows.Forms;
using System.Threading;

namespace MyApplication
{
// declaration of delegates
public delegate void SetStatusMessageDelegate(string message);
public delegate void CloseDialogDelegate();

///////////////////////////////////
/// the status dialog
public class StatusDialog : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;

private TextBox m_InfoText;

public SetStatusMessageDelegate m_SetStatusMessage;
public CloseDialogDelegate m_CloseDialog;
private System.Threading.ManualResetEvent m_Ready;

public StatusDialog(System.Threading.ManualResetEvent
dialogCreated)
{
this.m_SetStatusMessage = new
SetStatusMessageDelegate(SetStatusMessage);
this.m_CloseDialog = new CloseDialogDelegate(CloseDialog);
this.m_Ready = dialogCreated;
...
...
}

// signal that methods of the dialog can now be invoked
protected override void OnActivated(EventArgs e)
{
this.m_Ready.Set();
base.OnActivated (e);
}

private void SetStatusMessage(string message)
{
this.InfoText = message;
}

private void CloseDialog()
{
this.Close();
}

....
....
}

///////////////////////////////////
/// main app
public class MyForm : System.Windows.Forms.Form
{
private ManualResetEvent m_StatusDialogCreated;
private StatusDialog m_StatusDialog;
private System.Threading.Thread m_StatusDialogThread;

...
...

private void StatusDialogThread()
{
Application.Run(this.m_StatusDialog = new
StatusDialog(this.m_StatusDialogCreated));
}

public void OpenStatusDialog()
{
this.m_StatusDialogCreated = new ManualResetEvent(false);
this.m_StatusDialogThread = new Thread(new
ThreadStart(StatusDialogThread));
this.m_StatusDialogThread.Start();
// wait for the dialog to be ready
WaitHandle.WaitAll(new ManualResetEvent[]
{this.m_StatusDialogCreated});
}

public void SetStatusDialogMessage(string message)
{
if (this.m_StatusDialog!= null)
this.m_StatusDialog.BeginInvoke(this.m_StatusDialo g.m_SetStatusMessage,
new Object[]{message});
}

public void CloseStatusDialog()
{
if (this.m_StatusDialog!= null)
this.m_StatusDialog.BeginInvoke(this.m_StatusDialo g.m_CloseDialog,
null)
else // failsafe, dont want some half dead threads hanging
around - will work on this ;)
if (this.m_StatusDialogThread.ThreadState !=
ThreadState.Stopped) this.m_StatusDialogThread.Abort();

// activate this window
this.Activate();
}

...
}
}
----------------------------------------------------------------------------

That works for me...

Thanks for pointing me in the right direction,
Alexander
Nov 16 '05 #5

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

Similar topics

10
by: Charles Law | last post by:
I have a user control created on the main thread. Let's say, for arguments sake, that it has a single property that maintains a private variable. If I want to set that property from a worker...
5
by: Bill Davidson | last post by:
Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things, sinks events from outside sources. I realize the worker thread will...
3
by: zhaoyandong | last post by:
I have two threads, one UI thread, the other, the worker thread, which is responsible for keeping on search things and post the found results to a list control on the UI. My leader doesn't allow...
7
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it...
0
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I read from a serialport using a worker thread. Because the worker thread t does not loop often, I cannot wait to terminate the worker thread using a boolean in the While condition. So I have a...
0
by: Stonepony | last post by:
Hi you all, I'm writing an application where I have a C# GUI. The applicaiton can start a lot of worker threads in unmanaged C++. To get progress events the worker threads calles methods that...
6
by: kdikert | last post by:
I have a worker thread, and when the worker is done I want it to show a popup dialog. The sample application below demonstrates this. There are two buttons: a "Show dialog" button which immediately...
4
by: dgleeson3 | last post by:
Hello all Yes I know its been done before, but something silly is killing me on this. I have the standard progress bar and worker thread scenario with progress of the worker thread being fed...
3
by: Lawyno | last post by:
Hi there :) Here are some infos about my "project": I have the "honor" to write some scripts (VBScript) for some application developed by another company. In this application there is limited...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.