Winform应用程序的单例但提供参数?

发布于 2024-12-05 02:03:22 字数 1059 浏览 1 评论 0 原文

我有一个 webform(c#) 应用程序,它应该只能在单个实例中运行。同样重要的是,如果应用程序再次启动(单击应用程序图标),则新参数应转发到当前实例,但现在应用程序将启动。

我当然用谷歌搜索了这个问题并发现了这个: http://www.sanity-free.com /143/csharp_dotnet_single_instance_application.html

这就是我到目前为止的设置方式:

public class MyApp : ApplicationContext
{
        private static MyApp _instance;
        private static Mutex _mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");

        [STAThread]
        public static void Main(string[] args)
        {
             MyParams params;

             params = ExtractParams(args);

             if (_mutex.WaitOne(TimeSpan.Zero, true))
             {
                  _instance = new MyAppp(params);
                  Application.Run(_instance);
             }
             else
             {
                  _instance.SetParameters(params);
             }
        }
}

问题是我第二次尝试时要启动程序,我会在 else 中得到一个异常,即 _instance 为空?

我在这里做错了什么?

I have a webform(c#) application that should only be able to run in a single instance. Its also important that if the applcation is started again(click on app icon) then the new parameters should be forwarded to the current instance but now application are to be started.

I have ofcouse google the problem and found this : http://www.sanity-free.com/143/csharp_dotnet_single_instance_application.html

This is how I have set it up so far :

public class MyApp : ApplicationContext
{
        private static MyApp _instance;
        private static Mutex _mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");

        [STAThread]
        public static void Main(string[] args)
        {
             MyParams params;

             params = ExtractParams(args);

             if (_mutex.WaitOne(TimeSpan.Zero, true))
             {
                  _instance = new MyAppp(params);
                  Application.Run(_instance);
             }
             else
             {
                  _instance.SetParameters(params);
             }
        }
}

The problem with this is that the second time I try to start the program I will get an exception in the else that _instance is null?

What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

败给现实 2024-12-12 02:03:22

.NET 程序集 Microsoft.VisualBasic.dll 包含一个名为 WindowsFormsApplicationBase。该实例也可以由 C# 应用程序使用,并且有一个名为 IsSingleInstance 的属性,可以设置该属性来实现所需的行为。

在第一次应用程序启动时,您可以注册一个事件处理程序以供进一步的应用程序启动。从事件参数中,您可以提取每个附加启动的命令行参数。

请参阅此处的示例。

The .NET assembly Microsoft.VisualBasic.dll contains a class called WindowsFormsApplicationBase. This one can also be used by C# applications and has a property called IsSingleInstance, which can be set to achieve the required behaviour.

At first application start you can register an event handler for further application starts. From the event args you can extract command line parameters of every additional start.

See an example here.

享受孤独 2024-12-12 02:03:22

在本例中,它是进程间通信 (IPC),因此 _instance 没有意义。如果你想有IPC,有一些方法可以做到:socket、namepiped、messaging...

我认为有2种方法比较适合:

  1. 通过进程名获取上一个进程 ->调用P/Invoke EnumWindows方法来获取主窗口句柄。发送消息时带上之前已经用RegisterWindowMessage注册过的ID,来传递参数。
  2. 创建一个命名管道服务器来监听新进程的参数。

In this case, it's a Inter-process communication (IPC) so _instance doesn't sense. If you want to have IPC, there're some ways to do: socket, namepiped, messaging ...

I think 2 methods are suited:

  1. Get the previous process by the process name -> call P/Invoke EnumWindows method to get the main window handle. Send a message with ID has been registered with RegisterWindowMessage before, to pass parameters.
  2. Create a namedpipe server to listen for the parameters of the new process.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文