예제 #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            string id = Thread.CurrentThread.ManagedThreadId.ToString();

            Thread.CurrentThread.Name = "MainThread";
            richTextBox1.Text         = "主线程线程ID:" + id + "  主线程名:" + Thread.CurrentThread.Name + "\n";

            begin1   = new deltest1(method1);
            callback = new AsyncCallback(back1);

            List <objstate> list = new List <objstate>();

            for (int i = 0; i < 10; i++)
            {
                objstate obj = new objstate("James" + (i * i).ToString(), "Warke" + (i * 3).ToString());
                list.Add(obj);
            }
            //delegate.BeginInvoke(parameter[] para, AsyncCallback callback, object obj)
            //para是method方法的参数;
            //callback是method方法执行完后在同一子线程中立即执行的回调函数;
            //obj是一个可以传入回调函数中的object ,在回调函数中通过 IAsyncResult.AsyncState获取
            begin1.BeginInvoke(back1, list);

            this.Location = new System.Drawing.Point(0, 0);
            MessageBox.Show("主线程没有阻塞");
        }
예제 #2
0
 private void button1_Click(object sender, EventArgs e)
 {
     begin    = new deltest(method);
     callback = new AsyncCallback(back);
     obj      = new objstate();    //实例化类,该对象可以传入回调函数中
     begin.BeginInvoke(back, obj); //异步执行method,界面不会假死,5秒后执行回调函数,弹出提示框
 }
예제 #3
0
        private void back(IAsyncResult ar)
        {
            string   res = begin.EndInvoke(ar);
            objstate obj = (objstate)(ar.AsyncState);//通过AsyncState获取传入的object

            MessageBox.Show(res + "\n" + obj.fname + " " + obj.lname + " " + obj.birthday);
        }
예제 #4
0
 private void FirstMethod()
 {
     begin    = new deltest(method);
     callback = new AsyncCallback(back);
     obj      = new objstate();    //实例化类,该对象可以传入回调函数中
     begin.BeginInvoke(back, obj); //异步执行method,界面不会假死,5秒后执行回调函数,弹出提示框
 }