|
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.
Bsc. Of Computer Science.
Electronics practical engineer.
Developing distributed alarms management and video streaming systems.