我如何向用户显示等待/忙碌游标(通常是沙漏),让他们知道程序正在做什么?


当前回答

对于Windows窗体应用程序,可选的禁用ui控件是非常有用的。所以我的建议是这样的:

public class AppWaitCursor : IDisposable
{
    private readonly Control _eventControl;

    public AppWaitCursor(object eventSender = null)
    {
         _eventControl = eventSender as Control;
        if (_eventControl != null)
            _eventControl.Enabled = false;

        Application.UseWaitCursor = true;
        Application.DoEvents();
    }

    public void Dispose()
    {
        if (_eventControl != null)
            _eventControl.Enabled = true;

        Cursor.Current = Cursors.Default;
        Application.UseWaitCursor = false;
    }
}

用法:

private void UiControl_Click(object sender, EventArgs e)
{
    using (new AppWaitCursor(sender))
    {
        LongRunningCall();
    }
}

其他回答

使用下面的类,您可以使Donut的建议“异常安全”。

using (new CursorHandler())
{
    // Execute your time-intensive hashing code here...
}

类CursorHandler

public class CursorHandler
    : IDisposable
{
    public CursorHandler(Cursor cursor = null)
    {
        _saved = Cursor.Current;
        Cursor.Current = cursor ?? Cursors.WaitCursor;
    }

    public void Dispose()
    {
        if (_saved != null)
        {
            Cursor.Current = _saved;
            _saved = null;
        }
    }

    private Cursor _saved;
}

对于Windows窗体应用程序,可选的禁用ui控件是非常有用的。所以我的建议是这样的:

public class AppWaitCursor : IDisposable
{
    private readonly Control _eventControl;

    public AppWaitCursor(object eventSender = null)
    {
         _eventControl = eventSender as Control;
        if (_eventControl != null)
            _eventControl.Enabled = false;

        Application.UseWaitCursor = true;
        Application.DoEvents();
    }

    public void Dispose()
    {
        if (_eventControl != null)
            _eventControl.Enabled = true;

        Cursor.Current = Cursors.Default;
        Application.UseWaitCursor = false;
    }
}

用法:

private void UiControl_Click(object sender, EventArgs e)
{
    using (new AppWaitCursor(sender))
    {
        LongRunningCall();
    }
}

你可以使用:

Mouse.OverrideCursor = Cursors.Wait; 

&&

Mouse.OverrideCursor = Cursors.Arrow;

你可以使用Cursor.Current。

// Set cursor as hourglass
Cursor.Current = Cursors.WaitCursor;

// Execute your time-intensive hashing code here...

// Set cursor as default arrow
Cursor.Current = Cursors.Default;

但是,如果散列操作非常长(MSDN将其定义为超过2-7秒),则可能应该使用游标以外的可视反馈指示器来通知用户进度。有关更深入的指导方针,请参阅本文。

编辑: 正如@Am指出的那样,您可能需要调用Application.DoEvents();在游标。Current = Cursors.WaitCursor;以确保沙漏是真实显示的。

我的方法是在后台工作程序中进行所有的计算。

然后像这样改变光标:

this.Cursor = Cursors.Wait;

并且在线程的finish事件中恢复游标:

this.Cursor = Cursors.Default;

注意,这也可以用于特定的控件,所以只有当鼠标在沙漏上方时,光标才会是沙漏。