欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

利用多线程句柄设置鼠标忙碌状态的实现方法

程序员文章站 2024-02-16 15:06:58
当我们在读取数据的时候,或者处理大量数据的时候可能需要把鼠标设置为忙碌状态,等待返回结果。下面的代码可以帮忙实现这点:复制代码 代码如下:using system;usin...

当我们在读取数据的时候,或者处理大量数据的时候可能需要把鼠标设置为忙碌状态,等待返回结果。下面的代码可以帮忙实现这点:

复制代码 代码如下:

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;
}