检查当前的WPF应用程序是否正在运行?

时间:2023-01-18 18:32:13

How to check programmatically whether my WPF app is working or not right now?, Like App.PrevInstance in VB. Please suggest me a simple method, i am beginner.

如何以编程方式检查我的WPF应用程序现在是否正常工作?,与VB中的App.PrevInstance类似。请建议我一个简单的方法,我是初学者。

Here my code,

在这里我的代码,

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    var runningProcessByName = Process.GetProcessesByName("TCLCGFP");
    if (runningProcessByName.Length == 0)
    {
        //Process.Start("TCLCGFP.exe");
        new TraacsClcProcess();
    }
    else
    {
        MessageBox.Show("Application is already Running.","TCLCGFP",MessageBoxButton.OK,MessageBoxImage.Information);
    }
    Environment.Exit(0);
}

It is always showing Application is already Running :P

它始终显示应用程序已在运行:P

2 个解决方案

#1


2  

What most people do is use a named Mutex object. Use the constructor overload that tells you if it was successfully created to find out if another process has already created one with the same name. Example for using the mutex:

大多数人所做的是使用一个名为Mutex的对象。使用构造函数重载,该重载会告诉您是否已成功创建它以查明另一个进程是否已创建具有相同名称的进程。使用互斥锁的示例:

Mutex mutex;

try
{
   mutex = Mutex.OpenExisting("SINGLEINSTANCE");

   if (mutex!= null)
   {
         Console.WriteLine("Error, 1 instance Only");
         Application.Exit();
    }
 }
 catch (WaitHandleCannotBeOpenedException ex)
 {
        mutex  = new Mutex(true, "SINGLEINSTANCE");
 }

You can also read this How to determine if a previous instance of my application is running?

您还可以阅读本文如何确定我的应用程序的先前实例是否正在运行?

#2


0  

You can check process name as specified in the link I gave in the comment:

您可以检查我在评论中给出的链接中指定的进程名称:

public partial class App : System.Windows.Application
{
    public bool IsProcessOpen(string name)
    {
        foreach (Process clsProcess in Process.GetProcesses()) {
            if (clsProcess.ProcessName.Contains(name))
            {
                return true;
            }
        }
        return false;
    }

protected override void OnStartup(StartupEventArgs e)
{
    // Get Reference to the current Process
    Process thisProc = Process.GetCurrentProcess();
    if (IsProcessOpen("TCLCGFP.exe") == false)
    {
        //System.Windows.MessageBox.Show("Application not open!");
        //System.Windows.Application.Current.Shutdown();
    }
    else
    {
        // Check how many total processes have the same name as the current one
        if (Process.GetProcessesByName(thisProc.ProcessName).Length > 1)
        {
            // If ther is more than one, than it is already running.
            System.Windows.MessageBox.Show("Application is already running.");
            System.Windows.Application.Current.Shutdown();
            return;
        }
        base.OnStartup(e);
    }
}

#1


2  

What most people do is use a named Mutex object. Use the constructor overload that tells you if it was successfully created to find out if another process has already created one with the same name. Example for using the mutex:

大多数人所做的是使用一个名为Mutex的对象。使用构造函数重载,该重载会告诉您是否已成功创建它以查明另一个进程是否已创建具有相同名称的进程。使用互斥锁的示例:

Mutex mutex;

try
{
   mutex = Mutex.OpenExisting("SINGLEINSTANCE");

   if (mutex!= null)
   {
         Console.WriteLine("Error, 1 instance Only");
         Application.Exit();
    }
 }
 catch (WaitHandleCannotBeOpenedException ex)
 {
        mutex  = new Mutex(true, "SINGLEINSTANCE");
 }

You can also read this How to determine if a previous instance of my application is running?

您还可以阅读本文如何确定我的应用程序的先前实例是否正在运行?

#2


0  

You can check process name as specified in the link I gave in the comment:

您可以检查我在评论中给出的链接中指定的进程名称:

public partial class App : System.Windows.Application
{
    public bool IsProcessOpen(string name)
    {
        foreach (Process clsProcess in Process.GetProcesses()) {
            if (clsProcess.ProcessName.Contains(name))
            {
                return true;
            }
        }
        return false;
    }

protected override void OnStartup(StartupEventArgs e)
{
    // Get Reference to the current Process
    Process thisProc = Process.GetCurrentProcess();
    if (IsProcessOpen("TCLCGFP.exe") == false)
    {
        //System.Windows.MessageBox.Show("Application not open!");
        //System.Windows.Application.Current.Shutdown();
    }
    else
    {
        // Check how many total processes have the same name as the current one
        if (Process.GetProcessesByName(thisProc.ProcessName).Length > 1)
        {
            // If ther is more than one, than it is already running.
            System.Windows.MessageBox.Show("Application is already running.");
            System.Windows.Application.Current.Shutdown();
            return;
        }
        base.OnStartup(e);
    }
}