I have an app that doesn't show any forms most of the time, and I needed it to close by itself when a user was shutting down the system.
So I created a question, and the answer there, SystemEvents.SessionEnding, seemed to work.
At first.
Here is my Program.Main() method:
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
#region Pre-launch stuff
// Are we upgrading from an older version? We need to grab our old settings!
if (Properties.Settings.Default.UpgradeSettings)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpgradeSettings = false;
Properties.Settings.Default.Save();
}
// Visual styles
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
#if !DEBUG
// Add the event handler for handling UI thread exceptions to the event.
Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandling.Application_ThreadException);
// Set the unhandled exception mode to force all Windows Forms errors to go through our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// Add the event handler for handling non-UI thread exceptions to the event.
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(ErrorHandling.CurrentDomain_UnhandledException);
#endif
// Since we have no forms open we need to watch for the shutdown event; otherwise we're blocking it
SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_SessionEnding);
// ...
// ...
// ...
Application.Run();
}
The method SystemEvents_SessionEnding runs a method calling Exit(), which in turn runs:
public static void Exit()
{
MessageBox.Show("test");
try
{
IconHandler.SuperNotifyIcon.Dispose();
}
finally
{
Application.Exit();
}
}
And yet, my app blocks shutdown at times. And the message box that I added there doesn't show up.
I can't figure out how such a simple, simple path of execution could fail. But it does, and it's a major irritant for both me and my app's users.
Any ideas? Thoughts to throw around?
EDIT:
Per corvuscorax's answer, I've tried adding a form, but it's acted weird - at first, I had this in the form's code:
ShowInTaskbar = false;
WindowState = FormWindowState.Minimized;
Shown += (sender, e) => Hide();
And modified the Program.Exit() method to close that form, which would run the disposal code as well in the FormClosing event.
It turned out disabling ShowInTaskbar stopped the form from receiving a HWND_BROADCAST message. I've commented out that line now, but even so, I found that shutdown blocking was occurring. Once I clicked a button I made to show the form and tried to shut down again, it completed smoothly.
I would recommend against not having any forms at all.
Just do a normal WinForms application and set the main Form to be hidden and you get all the message processing working as always.
Addendum
As an added bonus of doing it this way, you also get a console for free while developing - by not hiding the main form, you can have an easy output view, simulate input, trigger actions with buttons, all sorts of useful stuff.
Addendum 2
Here's how I normally create invisible main forms. In the forms constructor, add this:
if (!Debugger.IsAttached)
{
// Prevent the window from showing up in the task bar AND when Alt-tabbing
ShowInTaskbar = false;
FormBorderStyle = FormBorderStyle.FixedToolWindow;
// Move it off-screen
StartPosition = FormStartPosition.Manual;
Location = new Point(SystemInformation.VirtualScreen.Right+10, SystemInformation.VirtualScreen.Bottom+10);
Size = new System.Drawing.Size(1, 1);
}
This will hide the window when not running from within the debugger.
Does your code have a message pump?
You need a message pump inside each user interface thread of your application so that any windows messages aimed at your application are dispatched and processed. Even if you never show a Form on the screen if the thread is a user interface thread when it will still be sent other system messages that would need to be dispatched and processed.
In a typical WinForms applications the message pump is inside this call...
Application.Run(new MyForm());
...which only exits once the MyForm instance is closed. In your case it seems you never make a call to the Application.Run method or anything similar. In which case your not handling windows messages. So what is your processing loop when you are not showing a Form?
Related
So I am creating an application that will have a splash screen that loads resources, and when complete will load the Main form of the application. In times past I've just let the splash screen own the thread and stay hidden, but I'm toying with the idea of actually letting my Main form be the thread owner, but I'm having trouble transitioning between forms. My current approach is
void main(String[] args)
{
ApplicationContext appContext = new ApplicationContext(new SplashScreen());
appContext.ThreadExit += appContext_ThreadExit;
Application.Run(appContext);
}
place
private void appContext_ThreadExit(object sender, EventArgs e)
{
Application.Run(new MainForm());
}
This give me the error that you can't start a new message loop on the thread. So how do I go about properly executing this transition? Or am I already using the best method by allowing the SplashScreen to own the thread?
Here is what I came up with, but if anyone has a more elegant or proper way to do it feel free to let me know.
bool isLoading = true;
MainForm.OnLoad()
-> creates and runs SplashScreen
MainForm.OnShow()
-> if isLoading is true, re-hide
SplashScreen.LoadingComplete
-> event to signal completion of loading events
MainForm.SplashScreenLoadingComplete
-> handler sets isLoading to false
-> calls Show() this time form will show
I handle the loading in the SplashScreen with a background worker
I want to implement a help-form into my app, which can get the focus, even if a dialog is shown. At the moment i dispose the actual instance of my help if it can't get focused, but i dont think thats the right way. So i want to ask, if theres a option to show a form, seperated from the logic of my main-application.
Things i tried:
calling as a AppDomain (MSDN)
putting the help into seperate app and call it as a process
In both ways, the help(-form) can't get the focus back, when a dialog was called.
I dont want to use the help provided with C#, because i need to show the help(-pages) inside the application.
Thanks
PS: I'm using .Net 2.0.
You can do this by creating an STA thread and using Application.Run() to display the form from that separate thread. Application.Run() will create a separate Message Pump for the other form; this is what keeps it separate.
If you do that, you have to be VERY CAREFUL when communicating between the forms. You will need to use Control.Invoke() or some other inter-thread mechanism to call UI-changing methods on the second form from the first form (and vice-versa).
But if you do this, then the first form can be showing a modal dialog, and the second form will still be focusable.
Note that the second window may be behind the first window because there will be no way to specify the relative Z-order between them.
Showing the second form can be done like this:
private static void ShowIndependentForm()
{
Thread thread = new Thread(ShowIndependentFormImpl);
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
}
private static void ShowIndependentFormImpl()
{
Application.Run(new Form2());
}
You can just call ShowIndependentForm() where appropriate; probably from the main form after you have created it, but my test code in Main() looks like this:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ShowIndependentForm();
Application.Run(new Form1());
}
Important
Because the second form has its own message pump, closing the first form will NOT close the program unless you set Thread.IsBackground to true. If you don't, you will have to explicitly close the second form (via calling a method in the second form using Control.Invoke() or some other way) when the first form closes if you want the program to close automatically.
I'm really new to Windows Forms programming and not quite sure what's the right way to go about programming.
This is my confusion.
I have a single form:
public partial class ReconcilerConsoleWindow : Form
{
public ReconcilerConsoleWindow()
{
InitializeComponent();
SetLogText("Started");
}
public void SetLogText(String text)
{
string logInfo = DateTime.Now.TimeOfDay.ToString() + ": " + text + Environment.NewLine;
tbx_Log.AppendText(logInfo);
}
}
And in my Program.cs class I have the following code:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ReconcilerConsoleWindow window = new ReconcilerConsoleWindow();
Application.Run(window);
if (CallSomeMethod() == true)
{
window.SetLogText("True");
}
}
}
Now, once the window has been displayed by the Application.Run command, the program halts at that point. How can I do further processing while the window is up?
The above is just an example. My purpose is to read an XMl file and display a datagridview. Subsequently, I watch the XMl file for changes and everytime a change is made, I want to refresh the datagridview. However, once the console pops up, how can i continue with my program and make changes to the information displayed on the form on the fly?
Processing that occurs after Application.Run is usually triggered in the form's Load event handler. You can easily create a Load method in Visual Studio by double clicking any open space on the form.
This would look something like this.
private void ReconcilerConsoleWindow_Load(object sender, EventArgs e)
{
if (CallSomeMethod())
{
this.SetLogText("True");
}
}
The reason this is (as stated in several other answers) is that the main thread (the one that called Application.Run(window)) is now taken up with operating the Message Pump for the form. You can continue running things on that thread through messaging, using the form's or forms' events. Or you can start a new thread. This could be done in the main method, before you call Application.Run(window), but most people would do this in Form_Load or the form constructor, to ensure the form is set up, etc. Once Application.Run returns, all forms are now closed.
Application.Run starts the Windows event handling loop. That loop won't finish til your form closes, at which time anything you do to it won't matter anyway.
If you want to do something with your form, do it in the form's Load event handler.
Program.cs is not meant to have business rules, it should only call your Form and display it. All datagrid loading/refreshing/editing should be done at your Forms. You should be using the Events defined on Forms class, like: OnLoad, OnUnload, OnClose and many others etc.
You are missing the concept. In a Windows Forms Application, your Main Thread is responsible for running the Form.
You can always use more Threads, but in Windows Forms I would recommend using a BackgroundWorker Component for parallel Tasks:
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Or a Timer:
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx
Once Application.Run(window) is called, you'll want to handle subsequent things within the application window itself.
In the code view of the form, find the following (or add it)
private void ReconcilerConsoleWindow_Load(object sender, EventArgs e)
{
//this is where you do things :)
if (CallSomeMethod() == true)
{
window.SetLogText("True");
}
}
I have encountered an odd issue with the way I am showing a splash form, that causes an InvalidAsynchronousStateException to be thrown.
First of all, here is the code for Main{} where I start the splash form:
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Thread splash = new Thread(new ThreadStart(ShowSplash));
splash.Start();
Application.Run(new MainForm());
}
static void ShowSplash()
{
using (SplashForm splash = new SplashForm())
{
Application.Run(splash);
}
}
I am using .NET2.0, with Win XP.
During some testing where the app was left running for may hours, I noticed that the number of exceptions would occasionally increase by one or two.
(Numbers obtained by PerfMon, viewing '# of Exceps Thrown' counter.) These exceptions seem to be caught and swallowed by the runtime, because they do
not ripple up and cause anything to go wrong in the app itself. At least nothing that I can determine anyway.
I have discovered that the exception is thrown when the UserPreferenceChanged event is fired by the system. Since finding this out, I can generate the exception
at will by changing the background picture or screen saver, etc.
I am not explicitly subscribing to this event myself anywhere in code, but I understand (via the power of Google) that all top level controls and forms subscribe
to this event automatically.
I still have not determined why this event is being fired in the first place, as it appears to happen while the app is running over night, but I guess that is another mystery to be solved.
Now, if I stop the splash form thread from running, the exception disappears. Run the thread, it comes back. So, it appears that something is not unsubscribing from the event, and this is causing the subsequent exception perhaps?
Interestingly, if I substitute my splash form with a default, out of the box Form, the problem still remains:
static void ShowSplash()
{
using (Form splash = new Form())
{
Application.Run(splash);
}
}
While this form is being displayed, any UserPreferenceChanged events do not cause any exceptions. As soon as the form is closed, and the thread exits, exceptions will be thrown.
Further research has lead me to this Microsoft article, that contains the following comment:
Common causes are a splash screens
created on a secondary UI thread or
any controls created on worker
threads.
Hmm, guilty as charged by the looks of it. Note that my app is not freezing or doing anything untoward though.
At the moment, this is more of a curiosity than anything else, but I am conecerned that there may be some hidden nasties here waiting to bite in the future.
To me, it looks like the form or the message pump started by Application.Run is not cleaning up properly when it terminates.
Any thoughts?
Yes, you are running afoul with the SystemEvents class. That class creates a hidden window that listens to system events. Particularly the UserPreferenceChanged event, a lot of controls use that event to know when they need to repaint themselves because the system colors were changed.
Problem is, the initialization code that creates the window is very sensitive to the apartment state of the thread that calls it. Which in your case is wrong, you didn't call Thread.SetApartmentState() to switch to STA. That's very important for threads that display a UI.
Beware that your workaround isn't actually a fix, the system events will be raised on the wrong thread. Your splash thread instead of the UI thread of your program. You'll still get random and extremely hard to diagnose failure when an actual system event gets fired. Most infamously when the user locks the workstation, the program deadlocks when it is unlocked again.
I think calling Thread.SetApartmentState() should fix your problem. Not 100% sure, these UI thread interactions are very difficult to analyze and I haven't gotten this wrong yet. Note that .NET already has very solid support for splash screens. It definitely gets details like this right.
I was able to simulate your problem and I can offer a work around, but there might be a better option out there since this was the first time I had come across this.
One option to avoid the exception is to not actuall close the splash screen, but rather just hide it. Something like this
public partial class SplashForm : Form
{
public SplashForm()
{
InitializeComponent();
}
// Not shown here, this is wired to the FormClosing event!!!
private void SplashForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
}
}
Then it will be important that you make the thread that you run the splash screen on a background thread to ensure that the application is not kept alive by the splash screen thread. So your code would look something like this
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Thread splash = new Thread(new ThreadStart(ShowSplash));
splash.IsBackground = true;
splash.Start();
Application.Run(new MainForm());
}
static void ShowSplash()
{
using (SplashForm splash = new SplashForm())
{
Application.Run(splash);
}
}
My program puts an icon in the system tray because the user may minimize to it. However, if the application crashes, or I stop the app from running in VS it leaves the icon in it until I hover over it with the mouse. Sometimes I'll look down there and there will be 10 or so icons.
I can I make sure the icon goes away?
There is no way to do this. Windows does not poll your program to see if it's still alive. Therefore, your icon will stay in the system tray until you either tell it to leave (normal exit), or the user does something that initiates a call to your program (such as mouseover). Only then does the shell know that your program has died and it needs to clean up your icon.
You have several options:
1) Write a root structured exception handler that ensures the destruction of the icon before exit. This one takes some effort - but you can basically add your own unhandled exception handler to Windows which will be called in the case of many forms of "boom" which will then allow you some last-ditch clean up opportunity.
2) Write a monitor application that sleeps on your main processes handle, and kills the icon when it wakes up (the main application has died). To make this latter scenario work well, you'll likely want the main process to NOT kill the icon itself - or you could create a way to tell the monitor app that it should exit w/o destroying the icon (have the monitor app sleep on both the process handle and a named event - and if woken by the event then it simply dies w/o cleaning up after the main app).
Before you exit, set the NotifyIcon Visible property to false.
You need to call Dispose on the NotifyIcon for it to leave the tray.
EDIT: There's an event you can hook into in your Program.cs. For all Windows apps, you'll have something like this:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
The Application class has a few events you can take advantage of to know when your app dies:
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
I don't know what happens when one kills the process, but if it crashes due to an exception, of course one can handle it. The way to do it best, depends on the type of application: Console, Forms, a service, ...
But in all cases, you should be able to use a try / finally structure in your Program.cs, and in the 'finally' section Dispose() the TrayIcon.
For example, in a Forms application, make your NotifyIcon (called TrayIcon in my example below) in your Form class public, and change the "Application.Run(new Form1())" line in Program.cs as follows:
Form form = new Form1();
try { Application.Run(form); }
finally { form.TrayIcon.Dispose(); }
We can hide trayIcon before form closing.
Add FormClosing Event
NotifyIcon ni;
...
...
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
ni.Visible = false;
}