My C# application is running at system startup, and must wait for the local SQL Server instance until it can actually do anything. Right now, I just spin waiting for the server to respond (I used to get a wait handle on the service, but that wasn't reliable), then launch the app's main dialog.
The problem with this, of course, is that the user can't tell anything is happening until the service starts, and because of the hardware we're using that can take up to a minute. So I'm thinking of throwing in a "Loading / Please Wait" indicator of some sort. The thing is, our project is nearing lockdown and a change as big as making a new class would cause a lot of headaches -- modifying an existing file (like Program.cs) is a lot less intrusive than making a new one. Long story short: is there a .NET class that would be well suited to being displayed (asynchronously, I guess) before I start plinking at the SQL Server, then removed when it starts to respond?
Here's a triple threaded version that I hacked together quickly that will do the trick. This can be dropped in anywhere in a visible form (or could be modified for program.cs) and will spawn a new, centered, modal dialog box with a smooth scrolling progress bar that will dominate user attention until FinishedProcessing in the parent thread is set to true.
//Update to true when finished loading or processing
bool FinishedProcessing = false;
System.Threading.AutoResetEvent DialogLoadedFlag
= new System.Threading.AutoResetEvent(false);
(new System.Threading.Thread(()=> {
Form StockWaitForm = new Form()
{ Name = "StockWaitForm", Text = "Please Wait...", ControlBox = false,
FormBorderStyle = FormBorderStyle.FixedDialog, StartPosition = FormStartPosition.CenterParent,
Width = 240, Height = 80, Enabled = true };
ProgressBar ScrollingBar = new ProgressBar()
{ Style = ProgressBarStyle.Marquee, Parent = StockWaitForm,
Dock = DockStyle.Fill, Enabled = true };
StockWaitForm.Load += new EventHandler((x, y) =>
{
DialogLoadedFlag.Set();
(new System.Threading.Thread(()=> {
while (FinishedProcessing == false) Application.DoEvents();
StockWaitForm.Invoke((MethodInvoker)(()=> StockWaitForm.Close()));
})).Start();
});
this.Invoke((MethodInvoker)(()=>StockWaitForm.ShowDialog(this)));
})).Start();
while (DialogLoadedFlag.WaitOne(100,true) == false) Application.DoEvents();
//
//Example Usage
//Faux Work - Have your local SQL server instance load here
for (int x = 0; x < 1000000; x++) int y = x + 2;
FinishedProcessing = true;
Customize to taste. Also, if you do use this in a production app, wrap the new thread contents in try...catch blocks to CYA. One last thing, I'm releasing this code to you under the "Coderer Public License / SO v1.1", as follows:
Coderer Public License / SO v1.0
I, Person known "Coderer" on the community "Stack Overflow", agree to thoroughly consider switching to a sane project management methodology that allows extra classes to be added to projects in the Executing phase. I understand that nazi-like change control is unhealthy for all parties involved.
Here's a .NET wrapper around the standard Windows progress dialog:
http://www.codeproject.com/KB/shell/iprogressdialognet.aspx
I haven't messed with it, but I'm sure there's a Flag to make the progress bar spin. If you want to get really fancy, you could even whip up your own AVI animation and plug it in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With