zoukankan      html  css  js  c++  java
  • A Sample of communication between two processes

    Some times, we like to talk about multi thread. But here today, I`d like to show a sample to explain the relationship of different process.

    To simplify the sample, we assume that each process include one thread process in itself.

    1. First show the main method

    As the following source Code, the main thread went to into  fModeDisp.ShowDialog() after it start a process. So there another thread was used to treat the process with FileName of sPrgName.

    1. Before start the p(new process), we  set  fModeDisp  into p.SynchronizingObject, so that we call make a connection between p and main thread.

    2. Then set fModeDisp.P_Exited into p.Exited, so that when the p process was exited, it will call into the fModeDisp.  

    public void DoProcess()

            {

                try
                {
                    this.Visible = false;
                    fModeDisp = new frmModeDisp();
                    System.Diagnostics.Process p = new System.Diagnostics.Process();
                    p.StartInfo.FileName = sPrgName;
                    p.SynchronizingObject = this;
                    p.SynchronizingObject = fModeDisp;
                    p.Exited += fModeDisp.P_Exited;
                    p.EnableRaisingEvents = true;
                    p.Start();
                    
                    fModeDisp.ShowDialog();
                    this.Close();
                }
                catch (Exception ex)
                { }
            }

    Now, let`s the P_Exited method in frmModeDisp class. Here this method will try to kill the fModeDisp class. 

    1. When the p thread callback into  frmModeDisp, the sender itself is the process in the main.  

    2. As  the process in DoProcess has subscribed before, "p.SynchronizingObject = fModeDisp;" have create a connection between p process and 

    the  frmModeDisp main thread. So at the end of P_Exited. We call this.Close(), so p process has the authority to utilize the resource in frmModeDisp .  Here is the key point. 

    public void P_Exited(object sender, EventArgs e)

            {
                try
                {
                    Process pr = (Process)sender;
                    bool proExisted = false;
                    foreach (var process in Process.GetProcesses())
                    {
                        if (process.Id == pr.Id || process.ProcessName == Process.GetCurrentProcess().ProcessName)
                             continue;
                        else if (filenames.Contains(process.ProcessName))
                            proExisted = true;
                    }
                    //When there is no refered exe in process, there is meaning to remain this Display Form.
                    //so kill it. 
                    if (!proExisted)
                        this.Close();
                }
                catch
                {
                }
            }
    Love it, and you live without it
  • 相关阅读:
    BZOJ.3884.上帝与集合的正确用法(扩展欧拉定理)
    HDU.5608.function(杜教筛)
    HDU.5628.Clarke and math(狄利克雷卷积 快速幂)
    51Nod.1244.莫比乌斯函数之和(杜教筛)
    SPOJ.Visible Lattice Points(莫比乌斯反演)
    BZOJ.2301.[HAOI2011]Problem B(莫比乌斯反演 容斥)
    BZOJ.2242.[SDOI2011]计算器(扩展欧几里得 BSGS)
    Codeforces757E.Bash Plays With Functions(积性函数 DP)
    插值法
    2、Windows下安装配置Redis
  • 原文地址:https://www.cnblogs.com/tomclock/p/7478876.html
Copyright © 2011-2022 走看看