예제 #1
0
        private void buttonRun_Click(object sender, EventArgs e)
        {
            PSO    pso       = new PSO();
            PSOCAL psoHandle = new PSOCAL(pso.demethod);

            MessageBox.Show("Begin!");
            watch.Reset();
            watch.Start();
            IAsyncResult result = psoHandle.BeginInvoke(new AsyncCallback(decallback), null);
        }
예제 #2
0
        public void decallback(IAsyncResult result)
        {
            //result 是“PSO.pso()方法”的返回值
            //AsyncResult 是IAsyncResult接口的一个实现类,空间:System.Runtime.Remoting.Messaging
            //AsyncDelegate 属性可以强制转换为用户定义的委托的实际类。
            //注意: BeginInvoke和EndInvoke必须成对调用.即使不需要返回值,但EndInvoke还是必须调用,否则可能会造成内存泄漏。
            PSOCAL decal = (PSOCAL)((AsyncResult)result).AsyncDelegate;

            if (result.IsCompleted)
            {
                watch.Stop();
                string time      = watch.Elapsed.ToString();
                string tempStr01 = "Solve the Problem!\n";
                string tempStr02 = string.Format("Using time is {0}", time);
                string finalStr  = tempStr01 + tempStr02;
                MessageBox.Show(finalStr);
            }
            decal.EndInvoke(result);
        }