C# 在 Windows 窗体程序“处理”时显示微调器,类似于 ajaxStart/ajaxStop?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9007152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Showing a spinner while a Windows Forms program is "processing", similar to ajaxStart/ajaxStop?
提问by bulltorious
Yes, yes. I know they are 2 completely different technologies. I recently moved over to FAT development with C# and Windows Forms from web development. I always thought it was very easy to show a spinner using ajaxStart and ajaxStop, so the user knows something is occurring and to continue waiting.
是的是的。我知道它们是两种完全不同的技术。我最近从 Web 开发转向了使用 C# 和 Windows 窗体进行 FAT 开发。我一直认为使用 ajaxStart 和 ajaxStop 显示微调器非常容易,因此用户知道发生了一些事情并继续等待。
Is there an equivalently simple technique for C# Windows Forms that can be implement globally? For example, if I am querying a DB and waiting for some results, normally the program becomes unresponsive for a few seconds and then starts working again after "processing". Is there some global way I can display a spinner while my program is "processing" instead of identifying all possible points in the program that could cause it to become unresponsive and just ripping off new threads?
对于 C# Windows 窗体,是否有可以全局实现的等效简单技术?例如,如果我正在查询数据库并等待一些结果,通常程序会在几秒钟内无响应,然后在“处理”后再次开始工作。是否有一些全局方式可以在我的程序“处理”时显示微调器,而不是识别程序中可能导致它变得无响应并只是扯掉新线程的所有可能点?
Please let me know if I am being unclear.
如果我不清楚,请告诉我。
采纳答案by Blorgbeard is out
The standard Windows Forms way to do that is Cursor.Current = Cursors.WaitCursor;.
执行此操作的标准 Windows 窗体方法是Cursor.Current = Cursors.WaitCursor;.
Does that work for you, or does it have to be an image?
这对您有用吗,还是必须是图像?
回答by Kristian
yes there is.
就在这里。
The easy was is to make two utility methods, one for enabling the "loading" indicator, and another for disabling that loading state.
简单的是制作两种实用方法,一种用于启用“加载”指示器,另一种用于禁用该加载状态。
Upon all initial ajax calls (or any other long calls), automatically call the enable method.
在所有初始 ajax 调用(或任何其他长调用)后,自动调用 enable 方法。
as soon as an oncomplete callback is fired, call the disable method from the oncomplete callback.
一旦 oncomplete 回调被触发,就从 oncomplete 回调中调用 disable 方法。
回答by antiduh
If your program becomes unresponsive, thats because you're doing real work on the UI thread. Move the work to a background thread, disable UI interaction as necessary to prevent the user from touching stuff while doing work, then when the background work finishes, ferry the results back to the UI thread using Control.Invoke, then update your UI with the results, then re-enable everything.
如果您的程序没有响应,那是因为您正在 UI 线程上进行实际工作。将工作移至后台线程,根据需要禁用 UI 交互以防止用户在工作时触摸内容,然后在后台工作完成后,使用 Control.Invoke 将结果传送回 UI 线程,然后使用结果,然后重新启用一切。
To implement the spinner, I use a PictureBox with a spinner gif in it. I disable it when the UI is idle, and enable it when I fire up the background worker.
为了实现微调器,我使用了一个带有微调器 gif 的图片框。我在 UI 空闲时禁用它,并在我启动后台工作程序时启用它。
The alternative is to change the mouse cursor to the 'waiting' cursor, which I'm not a huge fan of.
另一种方法是将鼠标光标更改为“等待”光标,我不是很喜欢。
回答by Joel
回答by Milton Quirino
Before processing : Cursor.Current = Cursors.WaitCursor;
处理前: Cursor.Current = Cursors.WaitCursor;
ProcessesSomething();
ProcessesSomething();
After processing : Cursor.Current = Cursors.AppStarting;
处理后: Cursor.Current = Cursors.AppStarting;
回答by Amirizzuan
I am using Visual Studio Enterprise 2015. The suggested cursor styles/types given here, none of them worked with me.
我正在使用 Visual Studio Enterprise 2015。此处给出的建议光标样式/类型,没有一个与我一起使用。
The one that is working with me is:
和我一起工作的是:
private async void button_Click(object sender, RoutedEventArgs e)
{
Cursor = Cursors.Wait; // change cursor to hourglass type
ThatProcessThatTookVeryLongTime();
Cursor = Cursors.Arrow; // change cursor to normal type
}
回答by Koray
This is the way I like to use when I need a wait cursor; instead of writing a try-finally code.
这是我在需要等待游标时喜欢使用的方式;而不是编写 try-finally 代码。
#region WaitCursor
public static IDisposable BeginWaitCursorBlock()
{
return ((!_waitCursorIsActive) ? (IDisposable)new waitCursor() : null);
}
private static bool _waitCursorIsActive;
private class waitCursor : IDisposable
{
private Cursor oldCur;
public waitCursor()
{
_waitCursorIsActive = true;
oldCur = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
}
public void Dispose()
{
Cursor.Current = oldCur;
_waitCursorIsActive = false;
}
}
#endregion
Example use:
使用示例:
using (BeginWaitCursorBlock())
{
...
}
回答by BJ Patel
Since .NET 4.5 (VS 2015+) you can use a combination of async and await with Progress for sending updates to UI thread:
从 .NET 4.5 (VS 2015+) 开始,您可以结合使用 async 和 await 与 Progress 将更新发送到 UI 线程:

