Reputation: 8350
I am using the below class to hide and show the windows task bar. I am calling the the class like below.
Problem : when i start the application, the taskbar is hiding perfectly. But when i exit the application by stopping the debugging, the task bar is not showing up. I mean the application exit is not firing in my code. I need such a way like, whatever the way i close my application, it shud finially show the tashbar() before exiting.
Please help. Thanks.
PROGRAM :
static class Program
{
[STAThread]
static void Main()
{
Taskbar.Hide();
Form1 TargerForm = new Form1();
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
Application.EnableVisualStyles();
Application.Run(TargerForm);
}
static void Application_ApplicationExit(object sender, EventArgs e)
{
Taskbar.Show();
}
}
CLASS :
public class Taskbar
{
[DllImport("user32.dll")]
public static extern int FindWindow(string className, string windowText);
[DllImport("user32.dll")]
public static extern int ShowWindow(int hwnd, int command);
public const int SW_HIDE = 0;
public const int SW_SHOW = 1;
public int _taskbarHandle;
protected static int Handle
{
get
{
return FindWindow("Shell_TrayWnd", "");
}
}
public Taskbar()
{
_taskbarHandle = FindWindow("Shell_TrayWnd", "");
}
public static void Show()
{
ShowWindow(Handle, SW_SHOW);
}
public static void Hide()
{
ShowWindow(Handle, SW_HIDE);
}
}
Upvotes: 1
Views: 1662
Reputation: 97691
When you stop debugging, that's a "rude" exit to a program, and nothing fires after that. MSFT's own Visual Studio integrated web browser suffers from this too. If this is super important to you, you might want to consider running something in the background which takes care of this cleanup for you and doesn't run in the context of the debugger?
I don't think there's a way to capture a "stop debugging" event, but I'd love to be proven wrong :)
Upvotes: 0
Reputation: 60021
Why not just call Taskbar.Show() after the call to Application.Run? Application.Run will block until the form is closed.
Your code could look like this:
[STAThread]
static void Main()
{
Taskbar.Hide();
Form1 TargerForm = new Form1();
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
Application.EnableVisualStyles();
Application.Run(TargerForm);
Taskbar.Show();
}
Upvotes: 1