如何在项目中创建ipc通信获胜表格

时间:2018-11-23 05:23:59

标签: c# winforms ipc

 public partial class Form1 : Form
 {
   private Process subProcess;
   Counter counter = new Counter();
   public Form1(string[] args)
    {
        InitializeComponent();

        if (args.Length == 0 || args[0].IndexOf("-subMonitor") == -1)
        {
            IpcServerChannel serverChannel = new IpcServerChannel("serverchannel");

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Counter), "counter", WellKnownObjectMode.Singleton);
            Console.WriteLine("This is call number " + counter.Count); 
            Console.WriteLine("This is call number " + counter.Count);
            Console.WriteLine("Listening on " + serverChannel.GetChannelUri());

            // MainProcess
            subProcess = new Process();

            subProcess.StartInfo.FileName = Assembly.GetExecutingAssembly().Location;
            subProcess.StartInfo.Arguments = "-subMonitor";
            subProcess.StartInfo.UseShellExecute = false;

            subProcess.Start();

            Button btn = new Button();
            btn.Size = new Size(100, 100);
            btn.Location = new Point(0, 0);
            this.Controls.Add(btn);
            btn.BringToFront();

            btn.MouseUp += new System.Windows.Forms.MouseEventHandler(btnMouseUp);
        }
        else
        {

            Button btn = new Button();
            btn.Size = new Size(100, 100);
            btn.Location = new Point(0, 0);
            this.Controls.Add(btn);
            btn.BringToFront();

            btn.MouseUp += new System.Windows.Forms.MouseEventHandler(btnMouseUp);
            // SubProcess
            //Console.WriteLine(args[0]);

            IpcClientChannel clientChannel = new IpcClientChannel();
            RemotingConfiguration.RegisterWellKnownClientType(typeof(Counter), "ipc://serverchannel/counter");

        }

    }

    private void btnMouseUp(object sender, MouseEventArgs e)
    {
        MessageBox.Show(counter.Count.ToString());
    }
}



public class Counter : MarshalByRefObject
{

    private static int count = 0;
    public int Count
    {
        get
        {
            return (count++);
        }
    }
}

我想通过ipc与同一程序通信。 执行该流程时,将打开同一子流程。

为了进行测试,我制作了一个有价值的Counter,即Singleton Object。 我想共享一个称为Count的单例对象。 但它不起作用。

我想要一个结果.....

如果我单击Main btn按钮,则结果为 2,3,4,5 如果我单击Sub btn按钮,则结果为 6,7,8

但是这个结果是....

如果我单击Main btn按钮,则结果为 2,3,4,5 如果我单击Sub btn按钮,则结果为 0,1,2 ...

它不是单例对象。

这是怎么了?

0 个答案:

没有答案
相关问题