Winform WaitScreen implementation - c#

I have the following WaitScreen class that shows a "please wait..." message when doing a background process:
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.ShowInTaskbar = 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 = "";
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);
}
}
}
The way I use it is:
waitScreen = new WaitScreen();
waitScreen.Show("Please wait...");
I have a MainForm, inside a mainform I have a button, when clicked I show a Dialog that on load will get some data from database in a Backgroundworker. Before running the backgroundworker I show my WaitScreen.
Its working great but when the WaitScreen is displayed and If I click on the back Dialog then the WaitScreen is gone. So I want to block so I can't click on the back Dialog until the worker has finish and then I close my WaitScreen.
Any clue on how to do that?
Thanks a lot.

I think you are overcomplicating this. What you want is a modal dialog window which the user can't close which will close when a given task is finished.
You can make a standard class which derives from Form and implements a constructor or a property which you can pass a Task or a callback.
Here's what the code could look like in your WaitScreen form:
public partial class WaitScreen : Form
{
public Action Worker { get; set; }
public WaitScreen(Action worker)
{
InitializeComponent();
if (worker == null)
throw new ArgumentNullException();
Worker = worker;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Task.Factory.StartNew(Worker).ContinueWith(t => { this.Close(); }, TaskScheduler.FromCurrentSynchronizationContext());
}
}
Here's what your code would look like in the consumer of this WaitScreen form:
private void someButton_Click(object sender, EventArgs e)
{
using (var waitScreen = new WaitScreen(SomeWorker))
waitScreen.ShowDialog(this);
}
private void SomeWorker()
{
// Load stuff from the database and store it in local variables.
// Remember, this is running on a background thread and not the UI thread, don't touch controls.
}
You probably want to use FormBorderStyle.None in the WaitScreen so that the user can't close it. Then the task is complete the WaitScreen will close itself and the caller will continue execution of code after the ShowDialog() call. ShowDialog() blocks the calling thread.

Related

How to invoke a method on the STA thread when you have nothing to invoke on?

This is rather a difficult situation to explain. I have a Windows Forms application that uses a notification icon with a context menu. The app can be configured to start with no form shown or the form can be closed by the user leaving just the notify icon present. Selecting the "Show" option from the context menu strip of the notification icon will create (if closed)/restore(if minimized) the main form. Now this all works fine as the events generated from the menu are running on the STA thread. The problem is the single instance implementation. A notify icon type application really should only ever be running one instance, otherwise you'd have multiple notify icons in the tray and it'd be a huge mess. This is accomplished using a named EventWaitHandle and also includes detection of an attempt to run a second instance, when this happens it signals the main running instance which then restores/creates the main form thus:
public static bool InitSingleInstance(this Control control, string handleName, Action? NewInstanceAttempt = null)
{
EventWaitHandle ewh = new(false, EventResetMode.ManualReset, handleName, out bool isNew);
if (isNew)
{
Task.Run(() =>
{
while (!control.IsDisposed)
{
ewh.WaitOne();
ewh.Reset();
NewInstanceAttempt?.Invoke();
}
});
}
else
{
Task.Run(() =>
{
EventWaitHandle.SignalAndWait(ewh, ewh, 100, true);
}).Wait();
}
return isNew;
}
The issue I have is that the loop waiting for a signal from a second instance runs in another Thread and thus needs to be delegated to the STA thread to restore/create the form. I use a UserControl to contain the NotifyIcon and Menu (So I can edit them in the designer) and this is created in Program.cs in place of Application.Run(new Form()) but this control is never actually shown itself so it never gets a Handle assigned to it that I can Invoke on so I get an exception when the second instance is run.
public partial class NotifyIconAndMenu : UserControl
{
private Form? mainForm = null;
private FormWindowState mainFormState = FormWindowState.Normal;
private readonly Func<Form> NewForm;
public NotifyIconAndMenu(Func<Form> newForm)
{
NewForm = newForm;
if(!this.InitSingleInstance("FOOBFOOB387846", NewInstanceAttempt))
return;
InitializeComponent();
Application.Run();
}
private void ShowMainForm()
{
if (mainForm == null || mainForm.IsDisposed)
{
mainForm = NewForm();
mainForm.Visible = true;
mainForm.Resize += MainForm_Resize;
}
mainForm.WindowState = mainFormState;
}
private void Quit()
{
Dispose();
Application.Exit();
}
private void MainForm_Resize(object? sender, EventArgs e)
{
if (mainForm != null && mainForm.WindowState != FormWindowState.Minimized)
mainFormState = mainForm.WindowState;
}
private void NewInstanceAttempt() => Invoke(ShowMainForm); // << exception
private void IconMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem == MI_Show) ShowMainForm();
else
if (e.ClickedItem == MI_Quit) Quit();
}
}
Perhaps there is a better way to do this? I did think of running a loop in the constructor waiting on a locked object and pulsing it from the other thread. Like this
public NotifyIconAndMenu(Func<Form> newForm)
{
NewForm = newForm;
if (!this.InitSingleInstance("NotIconDemo387846", NewInstanceAttempt))
return;
InitializeComponent();
lock (this)
{
while (!IsDisposed)
{
Monitor.Wait(this);
ShowMainForm();
}
}
}
private void NewInstanceAttempt()
{
lock(this)
{
Monitor.Pulse(this);
}
}
But this seems messy.
EDIT: And indeed wouldn't work as it would lock up the STA thread.
I managed to solve it like this:
public partial class NotifyIconAndMenu : UserControl
{
private Form? mainForm = null;
private FormWindowState mainFormState = FormWindowState.Normal;
private readonly Func<Form> NewForm;
public NotifyIconAndMenu(Func<Form> newForm)
{
NewForm = newForm;
if (!this.InitSingleInstance("GROWPLENTYOFCHEESE4444", NewInstanceAttempt))
return;
InitializeComponent();
FormShowLoop();
Application.Run();
}
private async void FormShowLoop()
{
while (!IsDisposed)
{
await Task.Run(() =>
{
lock (this)
{
Monitor.Wait(this);
}
});
if(!IsDisposed)
ShowMainForm();
}
}
private void ShowMainForm()
{
if (mainForm == null || mainForm.IsDisposed)
{
mainForm = NewForm();
mainForm.Resize += MainForm_Resize;
mainForm.Visible = true;
}
mainForm.WindowState = mainFormState;
}
private void Pulse()
{
lock (this)
{
Monitor.Pulse(this);
}
}
private void Quit()
{
Dispose();
Pulse();
Application.Exit();
}
private void MainForm_Resize(object? sender, EventArgs e)
{
if (mainForm != null && mainForm.WindowState != FormWindowState.Minimized)
mainFormState = mainForm.WindowState;
}
private void NewInstanceAttempt() => Pulse();
private void IconMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem == MI_Show) Pulse();
else
if (e.ClickedItem == MI_Quit) Quit();
}
}

DotNetZip Cancel extraction in Task

I have a method that executes a zip-extraction in a Task. Now I want the ability to Cancel the operation. But when I call the Cancel() method everything seems to stop immediately and the while runs forever:
public class OsiSourceZip
{
private const string ZipPassword = "******";
private bool _extractionDone;
private bool _cancelExtraction;
public async Task Extract(string sourceFile, string extractionDir)
{
Task extraction = Task.Run(() =>
{
using (ZipFile zipf = ZipFile.Read(sourceFile))
{
zipf.ExtractProgress += delegate(object sender, ExtractProgressEventArgs args)
{
args.Cancel = _cancelExtraction;
RaiseExtractionProgressUpdate(args);
};
zipf.Password = ZipPassword;
zipf.Encryption = EncryptionAlgorithm.WinZipAes256;
zipf.ExtractExistingFile = ExtractExistingFileAction.OverwriteSilently;
zipf.ExtractAll(extractionDir);
}
});
await extraction;
_extractionDone = true;
RaiseSourceInstallationCompleted();
}
public void Cancel()
{
_cancelExtraction = true;
while (!_extractionDone)
{
Thread.Sleep(500);
}
}
}
I've set a break point on args.Cancel = _cancelExtraction; but the event is not fired anymore as soon as the Cancel() method is called.
I've found a solution to this. I basically got rid of my own Cancel Method and do it as the dotnetzip Framework wants it. In the progress event.
Let's say you want to cancel the operation when the Form gets closed. I capture the FormClosing Event, cancel the close procedure and remember the close request. The next time the progress event fires, I set the the cancel property in the event args and close the Form myself in the completed Event:
public partial class MainForm : Form
{
private bool _closeRequested;
private void OnSourceInstallationCompleted(object sender, EventArgs e)
{
if (_closeRequested) { this.Close(); }
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (!_closeRequested)
{
_closeRequested = true;
e.Cancel = true;
}
}
private void OnExtractionProgressUpdate(object sender, ExtractProgressEventArgs e)
{
e.Cancel = _closeRequested;
}
}
I think it's rather ugly but it works....

Creating a timer winform application using only delegates and events [WITHOUT TIMER OBJECT]

While using timers, stopwatches and threads is the standard way, I was wondering if there was a way to create a Winform Application in c# which had a label with initial value as 0 and which automatically kept on incrementing once a button is clicked and when the same button is clicked again it should pause. Personally, I feel that the trick is to use multicast delegates. But I am stuck as to how to proceed.
NOTE: Possible use of method callback and InvokeRequired().
this code dose not use timer or stopwatch.
i have wrote a simple class for you, forgive me if its not so standard because im so lazy for now :)
public partial class Form1 : Form
{
CancellationTokenSource src;
CancellationToken t;
public Form1()
{
InitializeComponent();
}
//start incrementing
private async void button1_Click(object sender, EventArgs e)
{
this.Start.Enabled = false;
this.Cancel.Enabled = true;
this.src = new CancellationTokenSource();
this.t = this.src.Token;
try
{
while (true)
{
var tsk = Task.Factory.StartNew<int>(() =>
{
Task.Delay(500);
var txt = int.Parse(this.Display.Text) + 1;
return (txt);
}, this.t);
var result = await tsk;
this.Display.Text = result.ToString();
}
}
catch (Exception ex)
{
return;
}
}
// Stop incrementing
private void button1_Click_1(object sender, EventArgs e)
{
this.src.Cancel();
this.Cancel.Enabled = true;
this.Start.Enabled = true;
}
}
Really not sure why you think this can be done with your restrictions in place. If you want a delay in-between your "events", then you need to use some kind of Timer, or some kind of thread (classic Thread or some kind of Task) that has a delay within it...no way around that.
Here's another approach that'll probably violate your restrictions:
public partial class Form1 : Form
{
private Int64 value = -1;
private bool Paused = true;
private int IntervalInMilliseconds = 100;
private System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent(false);
public Form1()
{
InitializeComponent();
this.Shown += Form1_Shown;
}
private async void Form1_Shown(object sender, EventArgs e)
{
await Task.Run(delegate ()
{
while (true)
{
value++;
label1.Invoke((MethodInvoker)delegate ()
{
label1.Text = value.ToString();
});
System.Threading.Thread.Sleep(IntervalInMilliseconds);
mre.WaitOne();
}
});
}
private void button1_Click(object sender, EventArgs e)
{
if (Paused)
{
mre.Set();
}
else
{
mre.Reset();
}
Paused = !Paused;
}
}
USE an EVENT.
If you can not use timers or threads, then how about creating a do while loop that executes an event.
Some PSEUDO code is below - it should give you the idea..
bool IWantEvents = false;
public event EventHandler<myHandler> myNonTimerEvent ;
FormStart()
{
this.myNonTimerEvent += new MyNonTimerEventHandler();
IWantEvents = true;
Do
{
.. do some weird stuff - set IWantEvents False on condition ..
}
while(IWantEvents)
}
MyNonTimerEventHandler()
{
.. Do what I would do if I was using a timer event.
}

C# Singleton form not opening correctly from Timer, opening correctly from button press

I have a singletone form that can be opened from a ribbon button or that will check every minute whether it should be open after passing a few conditional checks.
When opening the form from the ribbon button, it works correctly every time.
When opening on the timer, the form does not get rendered correctly, any place a control should be is just displayed as a white rectangle. Screenshots below.
ThisAddIn.cs
using Timer = System.Timers.Timer;
public partial class ThisAddIn
{
private Timer ticker;
private void ThisAddIn_Startup(object sender, System.EventArgs e) {
ticker = new Timer(5 * 60 * 1000);
ticker.AutoReset = true;
ticker.Elapsed += new System.Timers.ElapsedEventHandler(checkForOverdue);
ticker.Start();
}
private void checkForOverdue(object sender, System.Timers.ElapsedEventArgs e)
{
bool overdue = false;
foreach (Reminder reminder in reminders)
{
DateTime now = DateTime.Now;
if (reminder.time <= now)
{
overdue = true;
break;
}
}
if (overdue)
{
RemindersList form = RemindersList.CreateInstance();
if (form != null)
{
form.Show();
}
}
}
}
Ribbon.cs
public partial class Ribbon
{
private void reminderListButton_Click(object sender, RibbonControlEventArgs e)
{
RemindersList form = RemindersList.CreateInstance();
if (form != null)
{
form.Show();
}
}
}
RemindersList.cs
public partial class RemindersList : Form
{
private static RemindersList _singleton;
private RemindersList()
{
InitializeComponent();
this.FormClosed += new FormClosedEventHandler(f_formClosed);
}
private static void f_formClosed(object sender, FormClosedEventArgs e)
{
_singleton = null;
}
public static RemindersList CreateInstance(List<Reminder> rs)
{
if (_singleton == null)
{
_singleton = new RemindersList(rs);
_singleton.Activate();
// Flash in taskbar if not active window
FlashWindow.Flash(_singleton);
return _singleton;
}
else
{
return null;
}
}
}
EDIT - SOLUTION
Per sa_ddam213's answer, I changed out the System.Timers.Timer for a Windows.Forms.Timer and it's now working just how I wanted.
Code changes:
ThisAddIn.cs
using Timer = System.Windows.Forms.Timer;
public partial class ThisAddIn {
private void ThisAddIn_Startup(object sender, System.EventArgs e) {
ticker = new Timer();
ticker.Interval = 5 * 60 * 1000;
ticker.Tick += new EventHandler(checkForOverdue);
ticker.Start();
}
// Also needed to change the checkForOverdue prototype as follows:
private void checkForOverdue(object sender, EventArgs e)
}
You can't touch UI controls/elements with any other thread than the UI thread, in your case the System.Timer is running on another thread and the window will never open
Try switching to a Windows.Forms.Timer
Or invoke the call back to the UI thread.
private void checkForOverdue(object sender, System.Timers.ElapsedEventArgs e)
{
base.Invoke(new Action(() =>
{
/// all your code here
}));
}
I suspect that the timer event handler is not launched on the UI thread, which could cause all sorts of problems. I would check that first and ensure that the UI stuff is actually done on the UI thread.

Show loading window

My application in WPF loads external resources, so I want to show a loading form while the program is loading.
I tried to create the form, and show before the loading code, and close when loading ended.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
LoadForm lf = new LoadForm();
lf.Visibility = Visibility.Visible;
// Al code that delays application loading
lf.Close();
}
But the only thing I get is that the form is showed when loading progress is complete and immediately closes.
I think that I need to use System.Threading but not sure.
Thanks.
Note I load all application external resources in Window_Loaded() method and not in the main class method.
You should look at this MSDN article on creating a SplashScreen in WPF. Essentially you add the Image you want to show to your project and set the Build Action to SplashScreen it will show when your program starts and disappear when your Main Application Window is shown.
You could also try importing the System.ComponentModel Class and use BackgroundWorker to Show your Loading Form, it will allow you to retain responsiveness of your UI.
public partial class MainWindow : Window
{
Window1 splash;
BackgroundWorker bg;
public MainWindow()
{
InitializeComponent();
bg = new BackgroundWorker();
bg.DoWork += new DoWorkEventHandler(bg_DoWork);
bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
}
void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
splash.Hide();
}
void bg_DoWork(object sender, DoWorkEventArgs e)
{
System.Threading.Thread.Sleep(10000);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
splash = new Window1();
splash.Show();
bg.RunWorkerAsync();
}
}
You should put your time consuming code in a background thread (for that you can use BackgroundWorker, Task or Async Await, depending on your dot net framework version)
private void Window_Loaded(object sender, RoutedEventArgs e)
{
LoadForm lf = new LoadForm();
lf.Visibility = Visibility.Visible;
//start the time consuming task in another thread
}
HeavyTaskCompleteEvent()
{
lf.Close();
}
Also look out for the best way to show loading screen. You can show some animation in the main window as well. I don't think showing a form is the best way.
I made a Loader class a while ago you could use. It shows a Window while doing your loading-method, closes it when completed and gives you the output of the method:
public class Loader<TActionResult>:FrameworkElement
{
private Func<TActionResult> _execute;
public TActionResult Result { get; private set; }
public delegate void OnJobFinished(object sender, TActionResult result);
public event OnJobFinished JobFinished;
public Loader(Func<TActionResult> execute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
}
private Window GetWaitWindow()
{
Window waitWindow = new Window { Height = 100, Width = 200, WindowStartupLocation = WindowStartupLocation.CenterScreen, WindowStyle = WindowStyle.None };
waitWindow.Content = new TextBlock { Text = "Please Wait", FontSize = 30, FontWeight = FontWeights.Bold, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };
return waitWindow;
}
public void Load(Window waitWindow = null)
{
if (waitWindow == null)
{
waitWindow = GetWaitWindow();
}
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate
{
Dispatcher.BeginInvoke(new Action(delegate { waitWindow.ShowDialog(); }));
Result = _execute();
Dispatcher.BeginInvoke(new Action(delegate() { waitWindow.Close(); }));
};
worker.RunWorkerCompleted += delegate
{
worker.Dispose();
if (JobFinished != null)
{
JobFinished(this, Result);
}
};
worker.RunWorkerAsync();
}
}
How to use it:
Loader<TResult> loader = new Loader<TResult>(MethodName);
loader.JobFinished += new Loader<TResult>.OnJobFinished(loader_JobFinished);
loader.Load();
void loader_JobFinished(object sender, TResult result)
{
// do whatever you want with your result here
}

Categories

Resources