How can i achieve only 1 instance of my WPF application to run and open the window on the task bar? I know there are a lot of question about this topic on the web, but none of them has a clear answer(or at least any answer has worked for me on WPF) of how to do it. I programed the answer gave on this question, here is my code:
private static Mutex mutex = new Mutex(true, "{1111werqwfwf}");
private static MainWindow mainWindow = null;
App()
{
InitializeComponent();
}
[STAThread]
static void Main()
{
if (mutex.WaitOne(TimeSpan.Zero, true))
{
App app = new App();
mainWindow = new MainWindow();
app.Run(mainWindow);
mutex.ReleaseMutex();
}
else
{
mainWindow.WindowState = WindowState.Normal;
}
}
The problem is that my MainWindow
is not opened. Also, i need to open the PointOfSale window which was the one minimized to the task bar, here is my code of this window (I am using NotifyIcon plugin):
public partial class PointOfSale : Window
{
TaskbarIcon tb;
public PointOfSale()
{
InitializeComponent();
tb = (TaskbarIcon)FindResource("NotifyIcon");
tb.DoubleClickCommand = new ShowWindowCommand();
tb.Visibility = System.Windows.Visibility.Visible;
Utils.Utils.SetTaskBarIcon(tb);
}
}
When the PointOfSale is closed, i check for the closing event and hide it:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Utils.Utils.pointOfSale = this;
Hide();
e.Cancel = true;
}
On the Utils.Utils.pointOfSale = this;
i save the instance of the actual window, with this i can open it when the icon on the task bar is double clicked.
Ay information about this would be helpful.
EDIT: I think it could also work if the last instance of the application could be killed and the new one instance could be created.