Click here to Skip to main content
15,881,709 members
Articles / Desktop Programming / Win32

Multi Process Architecture with C#

Rate me:
Please Sign up or sign in to vote.
4.80/5 (25 votes)
2 Jun 2009CPOL3 min read 147.7K   6.8K   129  
Separate functionality to run on child process
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Pipes;
using System.IO;

namespace MultiProcessIPCDemo
{
  class AppContext : ApplicationContext
  {
    private string m_ID;
    private NamedPipeClientStream m_PipeClientStream;
    System.Timers.Timer m_KeepAliveTimer = new System.Timers.Timer(2000);

    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="paramID"></param>
    public AppContext(string paramID)
    {
      // Handle the ApplicationExit event to know when the application is exiting.
      Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
      m_ID = paramID;
      
      StartIPC();

      m_KeepAliveTimer.Elapsed += new System.Timers.ElapsedEventHandler(m_KeepAliveTimer_Elapsed);
      m_KeepAliveTimer.Interval = 2000;
      m_KeepAliveTimer.Start();
    }   

    /// <summary>
    /// Sending message to the parent application
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void m_KeepAliveTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {      
      try
      {
        if (m_PipeClientStream.IsConnected)
        {
          StreamWriter sw = new StreamWriter(m_PipeClientStream);
          sw.WriteLine("Message from " + m_ID);
          sw.Flush();
          System.Diagnostics.Trace.WriteLine("KeepAlive was sent by " + m_ID);
        }        
      
      }
      catch(Exception ex)
      {        
        System.Diagnostics.Trace.WriteLine("pipeClient is disonnected for " + m_ID + "Error: " + ex.ToString());
        Application.Exit();
      }      
      
    }

    /// <summary>
    /// Exiting the process
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void Application_ApplicationExit(object sender, EventArgs e)
    {
      System.Diagnostics.Trace.WriteLine("Application Exit " + m_ID);
    }

    /// <summary>
    /// Connectin to the IPC server
    /// </summary>
    void StartIPC()
    {
      System.Diagnostics.Trace.WriteLine("Starting IPC client for " + m_ID);
      m_PipeClientStream = new NamedPipeClientStream(".", 
                                                m_ID,
                                                PipeDirection.InOut,
                                                PipeOptions.Asynchronous);      

      try
      {
        m_PipeClientStream.Connect(3000);
        System.Diagnostics.Trace.WriteLine("Connected to IPC server. " + m_ID);
      }
      catch(Exception ex)
      {
        System.Diagnostics.Trace.WriteLine("StartIPC Error for " + ex.ToString());
        return;
      }      
    }
  }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Israel Israel
Bsc. Of Computer Science.
Electronics practical engineer.

Developing distributed alarms management and video streaming systems.

Comments and Discussions