Inter-Process Communication

今天由于想找Firefox下的一个Google Docs插件,结果找到了GoogleDocListUploader,基于Google的Doc List API写的一个实例程序。这里有一篇讲解代码和设计的文章,虽然是个没有难度的工具,但作者的介绍非常细心。

有三个地方给我留下了印象:

  1. 使用Mutex控制程序只有一个的实例;
    Mutex mutex = new Mutex(true, "Local\\DocListUploader", out firstInstance);
    //Local保证了操作系统切换到其它用户身份登录时,可以开启程序的新实例
  2. 利用注册表操作向Windows Shell中添加右键菜单项;
  3. Inter-Process Communication(进程间通信)
    这是以前自己做WinForms时的弱项,.NET 2.0提供了IPC机制,找时间学习一下。
    //主进程监听后续进程
    public void ListenForSuccessor()
    {
        IpcServerChannel serverChannel = new IpcServerChannel("DocListUploader");
        ChannelServices.RegisterChannel(serverChannel, false);

        RemoteMessage remoteMessage = new RemoteMessage(this);
        RemotingServices.Marshal(remoteMessage,"FirstInstance");
       
    }
    //后续进程通知主进程
    public static void NotifyPredecessor(string file)
    {
        IpcClientChannel clientChannel = new IpcClientChannel();
        ChannelServices.RegisterChannel(clientChannel, false);

        RemoteMessage message = (RemoteMessage) Activator.GetObject(typeof(RemoteMessage),
          "ipc://DocListUploader/FirstInstance");

        if (!message.Equals(null))
        {
            message.SendMessage(file);
        }
    }
posted @ 2008-02-29 17:39  sinlight23  阅读(428)  评论(0编辑  收藏  举报