Friday, November 19, 2010

WinForms Wait Screen in C#

I know there are lots of waiting screens for Web UIs but this one for WinForms using Windows Applications

Special Thanks to Microsoft !

Public class WaitScreen
{
// Fields
private object lockObject = new object();
private string message = "Please Wait...";
private Form waitScreen;

// Methods
public void Close()
{
lock (this.lockObject)
{
if (this.IsShowing)
{
try
{
this.waitScreen.Invoke(new MethodInvoker(this.CloseWindow));
}
catch (NullReferenceException)
{
}
this.waitScreen = null;
}
}
}

private void CloseWindow()
{
this.waitScreen.Dispose();
}

public void Show(string message)
{
if (this.IsShowing)
{
this.Close();
}
if (!string.IsNullOrEmpty(message))
{
this.message = message;
}
using (ManualResetEvent event2 = new ManualResetEvent(false))
{
Thread thread = new Thread(new ParameterizedThreadStart(this.ThreadStart));
thread.SetApartmentState(ApartmentState.STA);
thread.Start(event2);
event2.WaitOne();
}
}

private void ThreadStart(object parameter)
{
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
ManualResetEvent event2 = (ManualResetEvent) parameter;
Application.EnableVisualStyles();
this.waitScreen = new Form();
this.waitScreen.Tag = event2;
this.waitScreen.ShowIcon = false;
this.waitScreen.AutoSize = true;
this.waitScreen.AutoSizeMode = AutoSizeMode.GrowAndShrink;
this.waitScreen.BackColor = SystemColors.Window;
this.waitScreen.ControlBox = false;
this.waitScreen.FormBorderStyle = FormBorderStyle.FixedToolWindow;
this.waitScreen.StartPosition = FormStartPosition.CenterScreen;
this.waitScreen.Cursor = Cursors.WaitCursor;
this.waitScreen.Text = SR.WaitScreenTitle;
this.waitScreen.FormClosing += new FormClosingEventHandler(this.WaitScreenClosing);
this.waitScreen.Shown += new EventHandler(this.WaitScreenShown);
Label label = new Label();
label.Text = this.message;
label.AutoSize = true;
label.Padding = new Padding(20, 40, 20, 30);
this.waitScreen.Controls.Add(label);
Application.Run(this.waitScreen);
Application.ExitThread();
}

private void WaitScreenClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
}
}

private void WaitScreenShown(object sender, EventArgs e)
{
Form form = (Form) sender;
form.Shown -= new EventHandler(this.WaitScreenShown);
ManualResetEvent tag = (ManualResetEvent) form.Tag;
form.Tag = null;
tag.Set();
}

// Properties
public bool IsShowing
{
get
{
return (this.waitScreen != null);
}
}
}


-------------------
How to use
-------------------

WaitScreen waitScreen = new WaitScreen();
waitScreen.Show("Please wait for me!");

6 comments:

Anonymous said...

It works. thanks,
but doesnt close itself when the expected window opens.

What could be wrong?

Aruna Tennakoon said...

Did u call the waitScreen.Close();?

Eg

WaitScreen waitScreen = new WaitScreen();
waitScreen.Show("Loading File. Please wait ...!");


// Do something here..

waitScreen.Close();
}

Anonymous said...

Thank you very much...

Anonymous said...

Thanks a lot

Anonymous said...

Create waiting bar in C# WinForms

Annonymous27 said...

Error CS0122: 'member' is inaccessible due to its protection level

At Line: this.waitScreen.Text = SR.WaitScreenTitle;

The wiggling red line is under: SR

Any help?

Thanks