0

With this code I can ensure that only one instance of the application is running. But I can not cause the window to be restored from the system tray. I tried window.Show() but not resolve.

    public class SingletonApplication : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
{
    public App application { get; private set; }

    Telas.wndLogin window;

    [STAThread]
    public static void Main(string[] args)
    {
        new SingletonApplication().Run(args);
    }

    public SingletonApplication()
    {
        this.IsSingleInstance = true;
    }

    protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
    {
        application = new App();

        window = new Telas.wndLogin();

        application.Run(window);

        return false;

    }

    protected override void OnStartupNextInstance(Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs eventArgs)
    {
       //todo: 
    }
}
noNerd
  • 3
  • 4
  • Not sure if singleton application is such a great idea. Possible dublicate. Check this out, maybe it helps: http://stackoverflow.com/questions/7182949/how-to-check-if-a-wpf-application-is-already-running – hschne Dec 18 '14 at 15:52
  • A window is not "on the system tray"... when you "minimize to tray", what are you doing with your window? Hiding it? Closing it? If you are closing it you need to create a new window... if you are hiding it, then `window.Show()` *should* work – Jcl Dec 18 '14 at 16:28
  • @Jcl i set this.ShowInTaskbar = true; this.Visibility = System.Windows.Visibility.Hidden; if try window.Visibility = System.Visibility.Visible; or window.Show() occours this error: **Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed.** – noNerd Dec 18 '14 at 16:51
  • Then it's obviously either being closed somewhere else, or you are targeting the wrong window – Jcl Dec 18 '14 at 16:57
  • Just the most obvious thing, but are you hiding it when the user clicks the X without preventing the closing? Also, `ShowInTaskbar` is not moving it to the tray. That decides if it should be seen on the taskbar (as it name implies), not on the tray – Jcl Dec 18 '14 at 17:00

0 Answers0