How can I provide my own Main() method in my WPF application?
and here
Simplest solution found:
- On App.xaml build properties, set "Build Action" to Page.
- Add following code to "App.xml.cs" or equivalent, the "App" class source.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
...
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static void Main()
{
// Use mutex to ensure only single instance is running
bool mutexOwnershipGranted = true;
using (Mutex mutex = new Mutex(
true,
"$SOMEUNIQUESTRING$", // Use GUID or string id here
out mutexOwnershipGranted))
{
// If this is the only running instance
if (mutexOwnershipGranted)
{ // Then run app
DevHelperWpf.App app = new DevHelperWpf.App();
app.InitializeComponent();
app.Run();
}
else
{ // Bring current running instance to the front
Process current = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(
current.ProcessName))
{
if (process.Id != current.Id)
{
SetForegroundWindow(process.MainWindowHandle);
break;
}
}
}
}
}
No comments:
Post a Comment