class a
{
  a()
  
  {
   //类数据初始话
  }
  public string str;
  a_do()  //该事件为1个死循环,每5秒左右触发一次
  {
    //数据处理过程
    str=处理好的数据;
  }
}class b
{
  a x=new a();
  //问题在这里,在class b里怎么才能取到class a_do()里处理好的数据str?不想在class b 里+计时器来循环读class b.str 的数据}

解决方案 »

  1.   

    参考一下下面的代码,修改并测试一下是否可以: /// <summary>
    /// 定义类的事件参数
    /// </summary>
    public class A_DoEventArgs : EventArgs
    {
    private string m_Str; public string Str
    {
    get { return m_Str; }
    }
    public A_DoEventArgs(string str)
    {
    this.m_Str = str;
    }
    }
    public class a
    {
    /// <summary>
    /// 声明类的事件
    /// </summary>
    public event EventHandler<A_DoEventArgs> A_Do;
    public a()
    {
    //类数据初始话 
    }
    public string str;
    void a_do()     //该事件为1个死循环,每5秒左右触发一次 
    {
    while (true)
    {
    //数据处理过程 
    str = "处理好的数据";
    if (A_Do != null)
    {
    //引发类的事件
    A_DoEventArgs args = new A_DoEventArgs(str);
    A_Do(this, args);
    }
    }
    }
    }
    public class b
    {
    public void somefun()
    {
    a x = new a();
    x.A_Do += new EventHandler<A_DoEventArgs>(x_A_Do);
    } void x_A_Do(object sender, A_DoEventArgs e)
    {
    //问题在这里,在class   b里怎么才能取到class   a_do()里处理好的数据str?不想在class   b   里+计时器来循环读class   b.str   的数据 
    string str = e.Str;
    }
    }
      

  2.   


    class   a 

        a() 
        { 
          //类数据初始话 
        } 
        public   string   str; 
        a_do()     //该事件为1个死循环,每5秒左右触发一次 
        { 
            get
            {
               //数据处理过程 
                str=处理好的数据; 
                if(this.Event != null)this.Event(str) 
             }
        } 
        public event EventDelegate Event;
    } delegate void EventDelegate(string data)class   b 

        a   x=new   a(); 
        //问题在这里,在class   b里怎么才能取到class   a_do()里处理好的数据str?不想在class   b   里+计时器来循环读class   b.str   的数据 
         b()
        {
            a.Event += new EventDelegate(this.Event);
        }
        
        void Event(string data)
        {
            //处理data的代码
        }
    }
      

  3.   

    每隔一段时间(定时)发生的事件,你可以用System.Timers.Timer这个组件来实现
    系统帮你维护一个线程来并行触发.相关详细讯息,请参考MSDN.
      

  4.   

    1 楼弱引用的方法就很好,每5秒左右触发一次的事件会给str赋值,之后马上触发 A_Do 事件,类 b 就能得到这个str,并且立即执行相关的代码。