国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

利用多線程句柄設置鼠標忙碌狀態(tài)的實現(xiàn)方法

2020-01-24 03:11:19
字體:
供稿:網(wǎng)友

當我們在讀取數(shù)據(jù)的時候,或者處理大量數(shù)據(jù)的時候可能需要把鼠標設置為忙碌狀態(tài),等待返回結(jié)果。下面的代碼可以幫忙實現(xiàn)這點:

復制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace CursorThread
{
    public partial class Form1 : Form
    {
        public delegate int DoSomethingDelegate(int data);

        public Form1()
        {
            InitializeComponent();
        }

        static int DoSomething(int data)
        {
            /// <sumary>
            /// Do something in this method
            /// </sumary>
            Thread.Sleep(300);
            return data++;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;

            DoSomethingDelegate d = DoSomething;
            IAsyncResult ar = d.BeginInvoke(100,null, null);

            while (true)
            {
                this.Cursor = Cursors.WaitCursor;
                if(ar.AsyncWaitHandle.WaitOne(50, false))
                {
                    this.Cursor = Cursors.Arrow;
                    break;
                }
            }

            //Get the result
            int result = d.EndInvoke(ar);
            MessageBox.Show(result.ToString());

        }
    }
}


這樣在點擊鼠標后,鼠標會變成忙碌狀態(tài)一直等待DoSomething這個方法調(diào)用結(jié)束,然后變回箭頭狀態(tài)。

當然你也可以這樣:

復制代碼 代碼如下:

// Set the status of the cursor
this.Cursor = Cursor.Busy;

// Do Something

// Set the status of the cursor
this.Cursor = Cursor.Arrow;


如果是在方法里面調(diào)用的話,不能使用this關(guān)鍵字,那你可以這樣做:
復制代碼 代碼如下:

private void Method()
{   
         Curosor.Current = Cursor.WaitCursor;

         /// Do Something

         Cursor.Current = Cursor.Arrow;
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 米脂县| 梧州市| 恩施市| 绥阳县| 崇左市| 安远县| 鹰潭市| 乌恰县| 萝北县| 大竹县| 广饶县| 千阳县| 醴陵市| 星座| 洪泽县| 佛山市| 巢湖市| 马尔康县| 天镇县| 西峡县| 景德镇市| 临湘市| 徐水县| 南开区| 怀化市| 绥棱县| 伊宁市| 五原县| 富蕴县| 淅川县| 聂拉木县| 龙井市| 临汾市| 阳山县| 赤水市| 嘉定区| 南宫市| 临沂市| 略阳县| 青浦区| 德清县|