当前位置: 移动技术网 > IT编程>开发语言>c# > 利用多线程句柄设置鼠标忙碌状态的实现方法

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
当我们在读取数据的时候,或者处理大量数据的时候可能需要把鼠标设置为忙碌状态,等待返回结果。下面的代码可以帮忙实现这点:复制代码 代码如下: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;
}

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网