利用多線程句柄設(shè)置鼠標忙碌狀態(tài)的實現(xiàn)方法
當我們在讀取數(shù)據(jù)的時候,或者處理大量數(shù)據(jù)的時候可能需要把鼠標設(shè)置為忙碌狀態(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;
}
相關(guān)文章
使用linq to xml修改app.config示例(linq讀取xml)
這篇文章主要介紹了使用linq to xml修改app.config示例,需要的朋友可以參考下2014-02-02
WPF利用CommunityToolkit.Mvvm實現(xiàn)級聯(lián)選擇器
這篇文章主要介紹了WPF如何利用CommunityToolkit.Mvvm實現(xiàn)級聯(lián)選擇器,文中的示例代碼講解詳細,對我們的學(xué)習(xí)或工作有一定幫助,需要的小伙伴可以參考一下2023-12-12
簡單聊聊C#字符串構(gòu)建利器StringBuilder
因為String類型代表不可變字符串,所以無法對當前String類型實例進行處理.所以FCL提供了System.Text.StringBuilder類型,下面這篇文章主要給大家介紹了關(guān)于C#字符串構(gòu)建利器StringBuilder的相關(guān)資料,需要的朋友可以參考下2022-03-03
C#實現(xiàn)漢字轉(zhuǎn)區(qū)位碼的示例代碼
區(qū)位碼是一個4位的十進制數(shù),每個區(qū)位碼都對應(yīng)著一個唯一的漢字,區(qū)位碼的前兩位叫做區(qū)碼,后兩位叫做位碼,下面我們就來看看如何使用C#實現(xiàn)漢字轉(zhuǎn)區(qū)位碼吧2024-01-01
C#用websocket實現(xiàn)簡易聊天功能(服務(wù)端)
這篇文章主要為大家詳細介紹了C#用websocket實現(xiàn)簡易聊天功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
C# 總結(jié)QueueUserWorkItem傳參幾種方式案例詳解
這篇文章主要介紹了C# 總結(jié)QueueUserWorkItem傳參幾種方式案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09
一文詳解C#中重寫(override)及覆蓋(new)的區(qū)別
這篇文章主要為大家詳細介紹了C#中重寫(override)及覆蓋(new)這兩個關(guān)鍵詞的區(qū)別,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-03-03
詳解C#中使用對象或集合的初始值設(shè)定項初始化的操作
這篇文章主要介紹了詳解C#中使用對象或集合的初始值設(shè)定項初始化的操作,文中分別講了對對象和字典的初始化,需要的朋友可以參考下2016-01-01

