Click here to Skip to main content
15,031,219 members
Articles / Desktop Programming / Win32
Posted 2 Jun 2009


125 bookmarked

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
Separate functionality to run on child process
Image 1


This article is about running modules in independent child processes to isolate them from the main application. Server applications make the use of external modules in order to extend functionality. The classic implementation to this case is to define base interfaces and use factory method classes to load and use the modules from class DLLs. The problem starts when the number of modules increases. Since they run in the context of the server application, the application becomes unsafe regarding unexpected failure in any of the extensions. In other words, crash in one of the extension modules will kill the main application. Multi-Process Architecture means that each driver runs in its own child process. This approach separates the modules from the main server application and also each module from the other.


There are few levels of module isolation.

The simplest way is using threading models. Each module runs in its own thread, while threading synchronization methods are used to access common resources. Threading makes the modules run concurrently but not so isolated.

.NET offers another way of isolation called AppDomain. Practically, it is different from threads in that it can be unloaded when misbehavior is detected. The drawback of AppDomain is that it stops the application when unhandled exception occurs. Using child process is the only way to protect the server application from unexpected behavior and failures in the modules.

The Multi-Process Architecture has been used lately by internet browsers that use tabs, like Internet Explorer 8 and Google Chrome. The display rendering functionality is isolated by using child process for each tab. It protects the browser application from misbehavior. Read more in Google Chrome Multi-process Architecture.


The main problems of using multi-process architecture are:

  1. Each module consumes resources of process. It means more memory and maybe some other issues that should be considered.
  2. Duplex inter-processing communication should be added to the module interface.

The Server

On the server side, the code uses System.Diagnostics.Process and System.IO.Pipe.NamedPipeServerStream classes to manage the child process life time.

In the demo, thread is started to begin communicating with the child process.

public bool Start(string paramUID)
      m_PipeID = paramUID;

      m_PipeMessagingThread = new Thread(new ThreadStart(StartIPCServer));
      m_PipeMessagingThread.Name = this.GetType().Name + ".PipeMessagingThread";
      m_PipeMessagingThread.IsBackground = true;

      ProcessStartInfo processInfo = new ProcessStartInfo
				("MultiProcessIPCClient", this.m_PipeID);
      m_ChildProcess = Process.Start(processInfo);

      return true;

    /// Start the IPC server listener and wait for
    void StartIPCServer()
      if (m_PipeServerStream == null)
        m_PipeServerStream = new NamedPipeServerStream(m_PipeID,

        //Wait for connection from the child process
        Console.WriteLine(string.Format("Child process {0} is connected.", m_PipeID));
      catch (ObjectDisposedException exDisposed)
        Console.WriteLine(string.Format("StartIPCServer for process {0} error: 
			{1}", this.m_PipeID, exDisposed.Message));
      catch (IOException exIO)
        Console.WriteLine(string.Format("StartIPCServer for process {0} error: 
			{1}", this.m_PipeID, exIO.Message));

      //Start listening for incoming messages.
      bool retRead = true; ;
      while (retRead && !m_IsDisposing)
        retRead = StartAsyncReceive();

The Child Process

The code example demonstrates how to create a windowless child process and how to communicate with the parent using System.IO.Pipe.NamedPipeClientStream.

The first step is to create a Windows Form application and then use the Run method with the ApplicationContext class as shown below:

static class Program
    /// The main entry point for the application.
    static void Main(string[] args)
      //Application.Run(new Form1());
      Application.Run(new AppContext(args[0]));

The AppContext implements ApplicationContext class that basically runs the message loop the same way as Windows.Form does. In this example, timer is started to send periodic 'Keep Alive' message to the parent application.

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

    // Constructor
    public AppContext(string paramID)
      // Handle the ApplicationExit event to know when the application is exiting.
      Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
      m_ID = paramID;


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

    // Sending message to the parent application
    void m_KeepAliveTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

    // Exiting the process
    void Application_ApplicationExit(object sender, EventArgs e)
      System.Diagnostics.Trace.WriteLine("Application Exit " + m_ID);

    // Connecting to the IPC server
    void StartIPC()
      System.Diagnostics.Trace.WriteLine("Starting IPC client for " + m_ID);
      m_PipeClientStream = new NamedPipeClientStream(".",

        System.Diagnostics.Trace.WriteLine("Connected to IPC server. " + m_ID);
      catch(Exception ex)
        System.Diagnostics.Trace.WriteLine("StartIPC Error for " + ex.ToString());

Inter-Process Communication (IPC)

The IPC method that I used is NamedPipe. In C#, we can use System.IO.Pipes from version .NET 3.5. In earlier frameworks, we must use wrappers for NamedPipe.

NamedPipe is a light weight approach for IPC. Using WCF, Remoting or simple networking is also acceptable.


The code example is just a simple demo that advices how to robust server and other applications that can't rely on third party implementations. The way Windows manages processes makes the modules run independently from the main application.

Moving to multi-process means doing some work, we must add IPC protocol instead of the existing simple interface. After I did this work in a large and busy server application, I must say that it was worth the effort, no more applications crash or hang.


  • 31st May, 2009: Initial version


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


About the Author

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

Developing distributed alarms management and video streaming systems.

Comments and Discussions

Questionquestion to the code Pin
abdou00729-Jul-13 23:50
Memberabdou00729-Jul-13 23:50 
AnswerRe: question to the code Pin
Marcus Mangelsdorf13-Aug-15 2:58
MemberMarcus Mangelsdorf13-Aug-15 2:58 
SuggestionParent / Child Processes Management Library for .NET Pin
Thomas Maierhofer (Tom)14-Jun-12 23:20
MemberThomas Maierhofer (Tom)14-Jun-12 23:20 
GeneralIPC DB Pin
pip01019-May-11 22:25
Memberpip01019-May-11 22:25 
GeneralRe: IPC DB Pin
Thomas Maierhofer (Tom)14-Jun-12 23:23
MemberThomas Maierhofer (Tom)14-Jun-12 23:23 
Generalhi Pin
Jayakrishnan Venkatramanan30-Sep-10 1:23
professionalJayakrishnan Venkatramanan30-Sep-10 1:23 
GeneralRe: hi Pin
Dan_Barack2-Oct-10 21:05
MemberDan_Barack2-Oct-10 21:05 
AnswerPerfect Pin
Steven Nicholas19-Jun-10 22:35
MemberSteven Nicholas19-Jun-10 22:35 
GeneralQuestion Pin
zoyzoy16-Mar-10 22:55
Memberzoyzoy16-Mar-10 22:55 
GeneralRe: Question Pin
Dan_Barack16-Mar-10 23:21
MemberDan_Barack16-Mar-10 23:21 
QuestionHow this implement work with a Windows or WPF application? Pin
Venkatesh Mookkan29-Sep-09 6:00
MemberVenkatesh Mookkan29-Sep-09 6:00 
This post is helpful. But I still don't have any clue to implement this in a Windows or WPF application.

Castle Rider
What if I freeze??? Don't forget to breath...

My: Website | Yahoo Group | Blog Spot

QuestionPipe Performance Pin
sm_imani25-Jun-09 4:44
Membersm_imani25-Jun-09 4:44 
AnswerRe: Pipe Performance Pin
Dan_Barack27-Jun-09 23:13
MemberDan_Barack27-Jun-09 23:13 
AnswerRe: Pipe Performance Pin
Thomas Maierhofer (Tom)14-Jun-12 23:25
MemberThomas Maierhofer (Tom)14-Jun-12 23:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.