作为标题状态,我想制作单个实例程序并在启动另一个实例时显示MainWindow。 我已经实现了显示只允许一个实例的消息。
public class MyApplication
{
static Mutex mutex = new Mutex(true, "FirstInstance");
[STAThread]
public static void Main(string[] args)
{
if (mutex.WaitOne(TimeSpan.Zero, true))
{
App app = new App();
app.InitializeComponent();
app.Run();
}
else
{
MessageBox.Show("only one instance at a time");
}
}
}
这很好但我想显示MainWindow而不是消息,所以我尝试了
static Application app;//change app to static
if (mutex.WaitOne(TimeSpan.Zero, true))
{
app = new App();
app.InitializeComponent();
app.Run();
}
else
{
app.MainWindow.WindowState = WindowState.Normal;
}
我得到“System.NullReferenceException:对象引用未设置为对象的实例”。应用程序中的MainWindow(静态)似乎为null,我不明白为什么。
所以我尝试了这篇文章http://sanity-free.org/143/csharp_dotnet_single_instance_application.html 但是WPF中不存在WndProc方法。
如果你能帮助我,我会很感激。 谢谢!
答案 0 :(得分:3)
我创建了一个示例WPF应用程序,只更改了App.xaml.cs文件。如果单实例窗口已经打开,则此代码将找到与当前进程的名称匹配的任何进程,如果该进程有窗口,则显示它:
public partial class App : Application
{
// signals to restore the window to its normal state
private const int SW_SHOWNORMAL = 1;
// create the mutex
private const string MUTEXNAME = "FirstInstance";
private readonly Mutex _mutex = new Mutex(true, MUTEXNAME);
public App()
{
if (!_mutex.WaitOne(TimeSpan.Zero))
{
ShowExistingWindow();
Shutdown();
}
}
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
// shows the window of the single-instance that is already open
private void ShowExistingWindow()
{
var currentProcess = Process.GetCurrentProcess();
var processes = Process.GetProcessesByName(currentProcess.ProcessName);
foreach (var process in processes)
{
// the single-instance already open should have a MainWindowHandle
if (process.MainWindowHandle != IntPtr.Zero)
{
// restores the window in case it was minimized
ShowWindow(process.MainWindowHandle, SW_SHOWNORMAL);
// brings the window to the foreground
SetForegroundWindow(process.MainWindowHandle);
return;
}
}
}
}
仅供参考,这在调试模式下不起作用,因为.vshost成为进程名称的一部分。如果需要它在调试模式下工作,则需要遍历所有进程而不是调用Process.GetProcessesByName。
答案 1 :(得分:0)
如果我没有误解你的问题,请尝试这样的事情
[STAThread]
public static void Main(string[] args)
{
Task task = new Task(() => { Thread.Sleep(200); MessageBox.Show("what a marvelous engineering"); });
task.Start();
//If you want application not to run untill task is complete then just use wait
task.Wait();
App app = new App();
app.InitializeComponent();
app.Run();
}
但我想知道你是否希望在你的MainWindow实例化之前完成你的C#工作,为什么不在App app = new App()之前做你的其他工作,让代码按顺序运行。