我如何向用户显示等待/忙碌游标(通常是沙漏),让他们知道程序正在做什么?
当前回答
我创建了一个静态异步方法。这将禁用启动操作并更改应用程序游标的控件。它将操作作为任务运行,并等待完成。控件在等待时返回给调用者。因此,即使在忙碌的图标旋转时,应用程序也能保持响应。
async public static void LengthyOperation(Control control, Action action)
{
try
{
control.Enabled = false;
Application.UseWaitCursor = true;
Task doWork = new Task(() => action(), TaskCreationOptions.LongRunning);
Log.Info("Task Start");
doWork.Start();
Log.Info("Before Await");
await doWork;
Log.Info("After await");
}
finally
{
Log.Info("Finally");
Application.UseWaitCursor = false;
control.Enabled = true;
}
这是主窗体的代码
private void btnSleep_Click(object sender, EventArgs e)
{
var control = sender as Control;
if (control != null)
{
Log.Info("Launching lengthy operation...");
CursorWait.LengthyOperation(control, () => DummyAction());
Log.Info("...Lengthy operation launched.");
}
}
private void DummyAction()
{
try
{
var _log = NLog.LogManager.GetLogger("TmpLogger");
_log.Info("Action - Sleep");
TimeSpan sleep = new TimeSpan(0, 0, 16);
Thread.Sleep(sleep);
_log.Info("Action - Wakeup");
}
finally
{
}
}
我不得不使用一个单独的日志记录器的虚拟动作(我使用Nlog)和我的主日志记录器写入用户界面(一个富文本框)。只有在表单上的特定容器上才能显示繁忙的游标(但我没有很努力地尝试)。所有控件都有一个UseWaitCursor属性,但它似乎对我尝试的控件没有任何影响(可能是因为它们不在顶部?)
这是主日志,它显示了事情按照我们预期的顺序发生:
16:51:33.1064 Launching lengthy operation...
16:51:33.1215 Task Start
16:51:33.1215 Before Await
16:51:33.1215 ...Lengthy operation launched.
16:51:49.1276 After await
16:51:49.1537 Finally
其他回答
我创建了一个静态异步方法。这将禁用启动操作并更改应用程序游标的控件。它将操作作为任务运行,并等待完成。控件在等待时返回给调用者。因此,即使在忙碌的图标旋转时,应用程序也能保持响应。
async public static void LengthyOperation(Control control, Action action)
{
try
{
control.Enabled = false;
Application.UseWaitCursor = true;
Task doWork = new Task(() => action(), TaskCreationOptions.LongRunning);
Log.Info("Task Start");
doWork.Start();
Log.Info("Before Await");
await doWork;
Log.Info("After await");
}
finally
{
Log.Info("Finally");
Application.UseWaitCursor = false;
control.Enabled = true;
}
这是主窗体的代码
private void btnSleep_Click(object sender, EventArgs e)
{
var control = sender as Control;
if (control != null)
{
Log.Info("Launching lengthy operation...");
CursorWait.LengthyOperation(control, () => DummyAction());
Log.Info("...Lengthy operation launched.");
}
}
private void DummyAction()
{
try
{
var _log = NLog.LogManager.GetLogger("TmpLogger");
_log.Info("Action - Sleep");
TimeSpan sleep = new TimeSpan(0, 0, 16);
Thread.Sleep(sleep);
_log.Info("Action - Wakeup");
}
finally
{
}
}
我不得不使用一个单独的日志记录器的虚拟动作(我使用Nlog)和我的主日志记录器写入用户界面(一个富文本框)。只有在表单上的特定容器上才能显示繁忙的游标(但我没有很努力地尝试)。所有控件都有一个UseWaitCursor属性,但它似乎对我尝试的控件没有任何影响(可能是因为它们不在顶部?)
这是主日志,它显示了事情按照我们预期的顺序发生:
16:51:33.1064 Launching lengthy operation...
16:51:33.1215 Task Start
16:51:33.1215 Before Await
16:51:33.1215 ...Lengthy operation launched.
16:51:49.1276 After await
16:51:49.1537 Finally
实际上,
Cursor.Current = Cursors.WaitCursor;
临时设置等待游标,但不确保等待游标直到操作结束才显示。程序中的其他程序或控件可以很容易地将光标重置回默认箭头,就像在操作仍在运行时移动鼠标一样。
一个更好的显示等待游标的方法是在表单中设置UseWaitCursor属性为true:
form.UseWaitCursor = true;
这将显示窗体上所有控件的等待游标,直到将此属性设置为false。 如果你想等待光标显示在应用程序级别,你应该使用:
Application.UseWaitCursor = true;
我的方法是在后台工作程序中进行所有的计算。
然后像这样改变光标:
this.Cursor = Cursors.Wait;
并且在线程的finish事件中恢复游标:
this.Cursor = Cursors.Default;
注意,这也可以用于特定的控件,所以只有当鼠标在沙漏上方时,光标才会是沙漏。
好的,其他人的观点很清楚,但我想补充一些,如下:
Cursor tempCursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
//do Time-consuming Operations
Cursor.Current = tempCursor;
在WPF中使用:
Cursor = Cursors.Wait;
// Your Heavy work here
Cursor = Cursors.Arrow;
推荐文章
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 没有ListBox。SelectionMode="None",是否有其他方法禁用列表框中的选择?
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何在iis7应用程序池中设置。net Framework 4.5版本
- 如何分裂()一个分隔字符串到一个列表<字符串>
- 如何转换列表<字符串>列表<int>?
- c#对象列表,我如何得到一个属性的和