问题就在消息队列里啊,
请给我mail,我给你发一个读取消息队列的消息的例子。
[email protected]

解决方案 »

  1.   

    算你走运,给你一个封装好的类,异步启动消息队列
    /// <summary>
    /// CommServer把内容对象从信息队列里取出
    /// </summary>
    public class CommServer
    {
    private static MessageQueue mq;//消息队列
    /// <summary>
    /// 构造函数
    /// </summary>
    public CommServer()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    /// <summary>
    ///启动异步接收消息
    /// </summary>
    /// <returns>0,正常。-1启动异常!</returns>
    public int Start ()
    {
    int errCode = 0;
    try
    {
    //1 得到消息队列
    mq = MQLib.MQDAO.GetDefaultQueue();
    //2 启动消息的接收代理。
    mq.ReceiveCompleted += new ReceiveCompletedEventHandler(MyReceiveCompleted);
              //3 开始异步接收
    mq.BeginReceive(); }
    catch(Exception e)
    {
    //异常抛出
    Debug.WriteLine(e.ToString());
    errCode = -1;
    }
    return errCode;
    } /// <summary>
    /// 停止接收消息队列里的消息。
    /// </summary>
    /// <returns>0,函数操作正常。-1表示函数操作异常</returns>
    public int End()
    {
    int errCode = 0;
    try
    {
    //1 去除消息队列的代理
    mq.ReceiveCompleted -= new ReceiveCompletedEventHandler(MyReceiveCompleted);
    //2 释放mq
    mq.Dispose();
    }
    catch(Exception e)
    {
    Debug.WriteLine(e.ToString());
    errCode = -1;
    }
    return errCode;
    }

    /// <summary>
    ///  处理消息队列的 ReceiveCompleted 事件。.
    /// </summary>.
    /// <param name="source"></param>
    /// <param name="asyncResult"></param>
    private void MyReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult)
    {
                 //1 Connect to the queue.
    mq = (MessageQueue)source;
    // 2 End the asynchronous Receive operation.
    Message m = mq.EndReceive(asyncResult.AsyncResult);
    m.Formatter = new XmlMessageFormatter(new Type[]{typeof(InputNotifyInfo)});
    YourNotifyInfo ReceiveNotifyInfo; try
    {
    ReceiveNotifyInfo = (YourNotifyInfo)m.Body ;
    }
    catch(Exception e)//转消息转换为YourNotifyInfo抛出异常,该消息为一个假的消息!!!
    {
    Debug.WriteLine("Error: " + e.ToString());
    return;
    } //3 Process message information  in ProcessNotify Function.
    ProcessNotify(ReceiveNotifyInfo);
    //4 Restart the asynchronous Receive operation.
    mq.BeginReceive();
    //5 Return
    return; 
    }

    /// <summary>
    /// Some Process
    /// </summary>
    /// <param name="info">通知消息对象</param>
    private void  ProcessNotify(YourNotifyInfo info)
    {
    //Check in value
    if( info == null) return;
    //SomeProcess();
    }
    }
      

  2.   

    启动时调用Start();
    结束时调用End();即可
      

  3.   

    我又试了试
    把COM+组件的代码直接移到线程过程里,没问题,可以用
    但是做成COM+组件,通过COM+调用,就把整个进程给堵掉了,
    无法从线称跳回主线称来.
      

  4.   

    http://www.microsoft.com/china/msdn/library/dv_vstechart/html/vbtchUsingThreads.asp学习线程中
      

  5.   

    我也有个例子using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using ClassLibrary;
    namespace Com_Test
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Label label4;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.label3 = new System.Windows.Forms.Label();
    this.label4 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(24, 56);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(72, 23);
    this.label1.TabIndex = 0;
    this.label1.Text = "初始化成功";
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(24, 128);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(64, 23);
    this.label2.TabIndex = 1;
    this.label2.Text = "管理成功";
    // 
    // label3
    // 
    this.label3.Location = new System.Drawing.Point(104, 56);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(100, 56);
    this.label3.TabIndex = 2;
    this.label3.Text = "label3";
    // 
    // label4
    // 
    this.label4.Location = new System.Drawing.Point(104, 128);
    this.label4.Name = "label4";
    this.label4.Size = new System.Drawing.Size(100, 64);
    this.label4.TabIndex = 3;
    this.label4.Text = "label4";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 272);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.label4,
      this.label3,
      this.label2,
      this.label1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e) {
    COMPlusServices client=new COMPlusServices(); 
    try{
                    this.label3.Text=client.DoTransaction(); 
    }catch(Exception e1){
    this.label3.Text=e1.Message.ToString(); 
    }
    try{
    client.IsManager();
                    this.label3.Text="能够管理"; 
    }catch(Exception e2){
    this.label4.Text=e2.Message.ToString() ;   
    }
    }
    }
    }