【问题标题】:Start application, but if its already started then run method on existing app/thread/proces启动应用程序,但如果它已经启动,则在现有应用程序/线程/进程上运行方法
【发布时间】:2019-10-15 07:40:44
【问题描述】:

我可以在启动时将参数传递给我的应用程序,效果很好!我还可以阻止应用程序的多个实例运行,这很棒。但是我试图将参数传递给应用程序,但如果应用程序已经在运行,那么在现有的 winform 上运行一个方法,而不是尝试打开一个新实例。我尝试过使用 application.openforms,但它似乎对我不起作用。

我尝试过使用类似这样的代码:

if (System.Windows.Forms.Application.OpenForms["MainMenu"] != null)
    {
        (System.Windows.Forms.Application.OpenForms["MainMenu"] as Form1).Output();
    }

程序.cs

static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
            {
                MessageBox.Show("BorisCRM is already running. Only one instance of this application is allowed");
            }
            else
            {
                if (args.Length > 0)
                    Application.Run(new Forms.MainMenu(args[0]));
                else
                    Application.Run(new Forms.MainMenu("NoArg"));
            }
        }

MainMenu Form:

        if (s != "NoArg")
        {
            NewForm frm = new NewForm(getclient);
            frm.MdiParent = this;
            frm.Show();
        }

这行得通.... 打开应用程序 - 打开菜单表单,我可以在其中选择使用菜单作为其 mdiparent 打开表单的菜单项。

这行得通.... open application with argument - 打开菜单表单,并使用菜单作为父 mdi 自动打开另一个表单。

这不起作用... 使用参数打开应用程序(当应用程序已经运行时) - 现有的菜单表单会自动打开另一个使用菜单作为父 mdi 的表单。

【问题讨论】:

标签: c# winforms


【解决方案1】:

Mutex 就是其中之一!!!谢谢亚历山德罗·德安德里亚

我用了这个例子:http://sanity-free.org/143/csharp_dotnet_single_instance_application.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多