1.一般情况h的剩余部分要等a返回后才执行,特殊情况得看你在a中是怎样触发h的

解决方案 »

  1.   

    http://www.csdn.net/develop/article/15/15850.shtm
      

  2.   

    1,
    jjcccc说: 一般情况h的剩余部分要等a返回后才执行,---- 就是说是同步
    cnhgj(戏子)说: 事件是异步,
    我:头大。能不能给个证据先?2,以下说法是我的理解,对不对?
    如果两个线程访问同一个全局变量,一定要加锁。要访问同一个函数,应该是给函数中的以下部分加锁:访问全局变量,静态变量,所在实例的成员变量。因为不同线程使用的是这些变量的同一个内存地址。
    而以下部分不用加锁:new分配的,动态局部变量。因为new分配的变量每个线程运行时都有新的分配(c++中变量名称作为指针实际是局部变量),而局部变量在堆栈中,每个线程有自己的堆栈,互不干扰。
    3,事件处理函数和触发事件的函数是否在同一线程?比如
    a(void)
    {
       event e;
       e();//触发事件
    }
    on_e(void)//事件处理函数
    {}
    函数 a 和 on_e 是否在同一线程中?
      

  3.   

    当然a 和 on_e 就在同一线程了
      

  4.   

    异步通知:轮询public void DoWork() {//...Console.WriteLine("Worker: work completed");if( completed != null ) {foreach( WorkCompleted wc in completed.GetInvocationList() ) {IAsyncResult res = wc.BeginInvoke(null, null);while( !res.IsCompleted ) System.Threading.Thread.Sleep(1);int grade = wc.EndInvoke(res);Console.WriteLine("Worker grade= " + grade);}}}【译注:下面给出本节例子完整代码:using System;delegate void WorkStarted();     delegate void WorkProgressing();delegate int WorkCompleted();class Worker {     public void DoWork()      {          Console.WriteLine("Worker: work started");          if( started != null ) started();          Console.WriteLine("Worker: work progressing");          if( progressing != null ) progressing();          Console.WriteLine("Worker: work completed");        if( completed != null )         {            foreach( WorkCompleted wc in completed.GetInvocationList() )             {                IAsyncResult res = wc.BeginInvoke(null, null);                while( !res.IsCompleted ) System.Threading.Thread.Sleep(1);                int grade = wc.EndInvoke(res);                Console.WriteLine("Worker grade= " + grade);            }        }     }     public event WorkStarted started ;     public event WorkProgressing progressing;     public event WorkCompleted completed;}class Boss {     public int WorkCompleted()      {          System.Threading.Thread.Sleep(3000);          Console.WriteLine("Better...");          return 6; /* out of 10 */     }}class Universe {     static void WorkerStartedWork()      {          Console.WriteLine("Universe notices worker starting work");     }     static int WorkerCompletedWork()      {          System.Threading.Thread.Sleep(4000);          Console.WriteLine("Universe is pleased with worker's work");         return 7;     }     static void Main()      {         Worker peter = new Worker();         Boss boss = new Boss();          peter.completed += new WorkCompleted(boss.WorkCompleted);          peter.started += new WorkStarted(Universe.WorkerStartedWork);          peter.completed += new WorkCompleted(Universe.WorkerCompletedWork);          peter.DoWork();          Console.WriteLine("Main: worker completed work");          Console.ReadLine();     }}/*以下是上段程序输出结果:Worker: work startedUniverse notices worker starting workWorker: work progressingWorker: work completedBetter...Worker grade = 6Universe pleased with worker's workWorker grade = 7Main: worker completed work //【译注:注意这个结果到最后才输出,下一节首句意思即是如此】*/】异步通知:委托public void DoWork() {//...Console.WriteLine("Worker: work completed");if( completed != null ) {foreach( WorkCompleted wc in completed.GetInvocationList() ) {wc.BeginInvoke(new AsyncCallback(WorkGraded), wc);}}}private void WorkGraded(IAsyncResult res) {WorkCompleted wc = (WorkCompleted)res.AsyncState;int grade = wc.EndInvoke(res);Console.WriteLine("Worker grade= " + grade);}【译注:下面给出本节例子完整代码:using System;delegate void WorkStarted();     delegate void WorkProgressing();delegate int WorkCompleted();class Worker {     public void DoWork()      {          Console.WriteLine("Worker: work started");          if( started != null ) started();          Console.WriteLine("Worker: work progressing");          if( progressing != null ) progressing();          Console.WriteLine("Worker: work completed");        if( completed != null )         {            foreach( WorkCompleted wc in completed.GetInvocationList() )             {                wc.BeginInvoke(new AsyncCallback(WorkGraded), wc);            }        }     }    private void WorkGraded(IAsyncResult res)     {        WorkCompleted wc = (WorkCompleted)res.AsyncState;        int grade = wc.EndInvoke(res);        Console.WriteLine("Worker grade= " + grade);    }     public event WorkStarted started ;     public event WorkProgressing progressing;     public event WorkCompleted completed;}class Boss {     public int WorkCompleted()      {          System.Threading.Thread.Sleep(3000);          Console.WriteLine("Better...");          return 6; /* out of 10 */     }}class Universe {     static void WorkerStartedWork()      {          Console.WriteLine("Universe notices worker starting work");     }     static int WorkerCompletedWork()      {          System.Threading.Thread.Sleep(4000);          Console.WriteLine("Universe is pleased with worker's work");         return 7;     }     static void Main()      {         Worker peter = new Worker();         Boss boss = new Boss();          peter.completed += new WorkCompleted(boss.WorkCompleted);          peter.started += new WorkStarted(Universe.WorkerStartedWork);          peter.completed += new WorkCompleted(Universe.WorkerCompletedWork);          peter.DoWork();          Console.WriteLine("Main: worker completed work");          Console.ReadLine();     }}/*以下是上段程序输出结果:Worker: work startedUniverse notices worker starting workWorker: work progressingWorker: work completedMain: worker completed work //【译注:异步委托发生了效果,因此这一行先输出啦J】Better...Worker grade = 6Universe pleased with worker's workWorker grade = 7*/事件是引发的,
    是同步的,
    可以通过事件(本身是被特殊支持的对象)的方法来实现异步。