我应该在后台线程中运行所有内容吗?
我正在设计一个应用程序,该应用程序在等待来自服务器(数据库或互联网)的数据时可能会挂起,问题是我不知道如何最好地应对众多不同地方的事情可能需要时间。
我很高兴在访问发生时向用户显示“正在加载”对话框,但理想情况下我不希望它在短期运行操作中弹出并消失。
Microsoft Word 似乎很好地处理了这个问题,就好像您单击一个按钮并且操作需要很长时间,几秒钟后您会看到一个“正在工作..”对话框。该操作仍然是同步的,您无法中断操作。但是,如果相同的操作很快发生,您显然不会收到该对话框。
我很高兴设计一些通用的后台工作线程处理程序,并且 99% 的数据处理已经在静态原子方法中完成,但如果可以的话,我想在这方面采用最佳实践。
如果有人有模式、代码或建议,我欢迎他们所有人
干杯
I am designing an application which has the potential to hang while waiting for data from servers (either Database or internet) the problem is that I don't know how best to cope with the multitude of different places things may take time.
I am happy to display a 'loading' dialog to the user while access is happening but ideally I don't want this to flick up and disappear for short running operations.
Microsoft word appears to handle this quite nicely, as if you click a button and the operation takes a long time, after a few seconds you get a 'working..' dialog. The operation is still synchronous and you can't interrupt the operation. However if the same operation happens quickly you obviously don't get the dialog.
I am happy(ish) to devise some generic background worker thread handler and 99% of my data processing is already done in static atomic methods, but I would like to go best practice on this one if I can.
If anyone has patterns, code or suggestions I welcome them all
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我肯定会考虑异步使用具有 2 个事件的模式。第一个“事件”是,您实际上从任何地方/任何时间都获得了数据,而您必须等待它。第二个事件是延迟计时器。如果您在此计时器弹出之前获得数据,则一切都很好。如果没有,那么您会弹出“我很忙”并允许他们取消请求。通常取消只是意味着当您最终得到回复时“忽略”。
I would definitely think asynchronously using a pattern with 2 events. The first "event" is that you actually got your data from wherever/whenever you had to wait for it. The second event is a delay timer. If you get your data before this timer pops, all is well and good. If not, THEN you pop up your "I'm busy" and allow them to cancel the request. Usually cancel just mean "ignore" when you finally get the response.
如果这是您想要的行为...
您可以通过在BackgroundWorker 周围包装一个类来相当轻松地处理这个问题。只需计算 DoWork 事件的开始时间以及第一份进度报告的时间即可。如果经过一定时间,您可以显示对话框 - 否则,阻止(因为这是一个简短的过程)。
话虽这么说,任何时候您正在做可以异步处理的工作,我都建议您这样做。最好不要在明显的时间间隔内阻塞您的 UI,即使时间很短。在 .NET 4(或带有 Rx 框架的 3.5)中,通过使用 任务并行库。
If this is the behavior your want...
You could handle this, fairly easily, by wrapping a class around BackgroundWorker. Just time the start of the DoWork event, and the time to the first progress report. If a certain amount of time passes, you could show your dialog - otherwise, block (since it's a short process).
That being said, any time you're doing work that can be processed asynchronously, I'd recommend doing it that way. It's much nicer to never block your UI for a noticable interval, even if it's short. This becomes much simpler in .NET 4 (or 3.5 with Rx framework) by using the task parallel library.
理想情况下,您应该在后台线程中或异步运行任何 IO 或非 UI 处理,以避免锁定 UI。
Ideally you should be running any IO or non-UI processing either in a background thread or asynchronously to avoid locking up the UI.