现在的位置: 首页 > 综合 > 正文

VS2005下简单的进程间通信实例

2012年09月03日 ⁄ 综合 ⁄ 共 3242字 ⁄ 字号 评论关闭

        转眼又是两个多月没上来了,最近在做一个项目,用到了进程间通信,最开始想用API,了解了几种Windows下进程通信的技术,结果忽然发现了一个好东西 - IPC信道,这个东西虽然只能支持本机间的进程通信,但对于我的需求来说,也足够了
 
        我的需求基本上是这样的:服务器端和客户端各一个进程,在服务器端进程里,又有多个线程;客户端进程要对服务器端进程里的这几个线程的运行状态进行监视,并以列表的形式将服务器端进程里线程的状态显示出来

        首先,我们看看MSDN上信道的概念:信道是跨远程处理边界(无论是在应用程序域、进程还是计算机之间)在应用程序之间传输消息的对象。信道可以在终结点上侦听入站消息,向另一个终结点发送出站消息,或者两者都可以。这使您能够插入各种各样的协议,即使信道的另一端上没有公共语言运行库

        System.Runtime.Remoting.Channels.Ipc 命名空间定义用于远程处理的通信信道,该信道使用 Windows 操作系统的进程间通信 (IPC) 系统。由于不使用网络通信,IPC 信道比 HTTP 和 TCP 信道快得多,但它只能用在同一物理计算机上的应用程序域之间进行通信。

        本例就使用IPC进行信道通信,虽然使用WebService也可以完成这个任务,又比较流行:),但流行的东西不一定放到哪里都好用呀,呵呵

        为了进行数据通信,先建立一个类,这个类要继承MarshalByRefObject

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.ComponentModel;

namespace CData
{
    
public class Remote : MarshalByRefObject
    
{

        
public static IList<Test> _ht = new List<Test>();

        
public IList<Test> Ht
        
{
            
get
            
{
                
return (IList<Test>) _ht;
            }

            
set
            
{
                _ht 
= (IList<Test>)value;
            }


        }


        [Serializable]
        
public class Test
        
{
            
private string _threadname = "";
            
private int _status = 0;

            [DisplayName(
"线程名")]
            
public string ThreadName
            
{
                
get
                
{
                    
return this._threadname;
                }

                
set
                
{
                    
this._threadname = value;
                }



            }


            [DisplayName(
"状态")]
            
public int Status
            
{
                
get
                
{
                    
return this._status;
                }

                
set
                
{
                    
this._status = value;
                }


            }

        }

    }

}

        
        其中的Test类是一个IList类型,用来保存服务器进程里各线程的运行状态,_ht为静态数据,以便在客户端进程里建立这个实例时,可以取到值

        以下为服务端进程的代码:

        可以窗体Load事件或按钮事件里添加方法

IpcChannel serverChannel = new IpcChannel("localhost");

            ChannelServices.RegisterChannel(serverChannel, 
true); 

            RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Remote), "Remote.rem", WellKnownObjectMode.Singleton);

            Remote.Test t 
= new Remote.Test();

            t.ThreadName 
= "Thread";

            Remote._ht.Add(t);

            Remote.Test tt 
= new Remote.Test();

            tt.ThreadName 
= "Thread1";
            tt.Status 
= 1;


            Remote._ht.Add(tt);

        
        这里将相关线程的名字及状态加到了用于数据通信的类里(其实,这里并没有建立任何新的线程)下边我们看看客户端那边的代码
 

IpcChannel channel = new IpcChannel();

        System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(channel,
true);

        System.Runtime.Remoting.WellKnownClientTypeEntry remoteType 
= new System.Runtime.Remoting.WellKnownClientTypeEntry(
        
typeof(Remote),"ipc://localhost/Remote.rem");
        
    System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownClientType(remoteType);

        Remote client 
= (Remote)Activator.GetObject(typeof(Remote), "ipc://localhost/Remote.rem");

        
this.dataGridView1.DataSource = client.Ht;     //这里假设有一个DataGridView

        
        实际的运行结果就是在客户端的DataGridView里,显示出来了服务器端的数据结果
        当然,线程的状态是变化的,实际应用时,服务端的代码可能会有变化,因为我们总是要反映出真正的各线程状态,不然程序也就没有意义了,呵呵!

        对了,有需要实例程序的可以Email我

抱歉!评论已关闭.