這個示例是在Winform裏作的,你們能夠參考:異步
- //定義聲明委託
- private delegate string returnresult(int i, long b);
- /// <summary>
- /// 同步執行事件
- /// </summary>
- private void button3_Click(object sender, EventArgs e)
- {
- returnresult sl = sss;
- string ssa = sl.Invoke(0, 1000000101);//只有等待
- MessageBox.Show(ssa);
- }
- private string sss(int a, long b)
- {
- long s = 0;
- for (long o = a; o < b; o++)
- {
- s += o;
- }
- return s.ToString().Trim();
- }
- /// <summary>
- /// 異步執行時間
- /// </summary>
- private void button5_Click(object sender, EventArgs e)
- {
- returnresult dd = sss;
- AsyncCallback sas = new AsyncCallback(asiresult);
- dd.BeginInvoke(0, 1000000101, sas, dd);//照樣運行
- MessageBox.Show("照樣運行!!!");
- }
- /// <summary>
- /// 回調函數
- /// </summary>
- /// <param name="asa"></param>
- private void asiresult(IAsyncResult asa)
- {
- returnresult aass = asa.AsyncState as returnresult;
- MessageBox.Show(aass.EndInvoke(asa));
- }