利用多线程句柄设置鼠标忙碌状态的实现方法
当我们在读取数据的时候,或者处理大量数据的时候可能需要把鼠标设置为忙碌状态,等待返回结果。下面的代码可以帮忙实现这点:
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());
}
}
}
这样在点击鼠标后,鼠标会变成忙碌状态一直等待DoSomething这个方法调用结束,然后变回箭头状态。
当然你也可以这样:
// Set the status of the cursor
this.Cursor = Cursor.Busy;
// Do Something
// Set the status of the cursor
this.Cursor = Cursor.Arrow;
如果是在方法里面调用的话,不能使用this关键字,那你可以这样做:
private void Method()
{
Curosor.Current = Cursor.WaitCursor;
/// Do Something
Cursor.Current = Cursor.Arrow;
}
相关文章
使用linq to xml修改app.config示例(linq读取xml)
这篇文章主要介绍了使用linq to xml修改app.config示例,需要的朋友可以参考下2014-02-02
WPF利用CommunityToolkit.Mvvm实现级联选择器
这篇文章主要介绍了WPF如何利用CommunityToolkit.Mvvm实现级联选择器,文中的示例代码讲解详细,对我们的学习或工作有一定帮助,需要的小伙伴可以参考一下2023-12-12
C# 总结QueueUserWorkItem传参几种方式案例详解
这篇文章主要介绍了C# 总结QueueUserWorkItem传参几种方式案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下2021-09-09
一文详解C#中重写(override)及覆盖(new)的区别
这篇文章主要为大家详细介绍了C#中重写(override)及覆盖(new)这两个关键词的区别,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下2023-03-03


最新评论