1

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

4

3 回答 3

3

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.

于 2010-02-15T19:02:13.280 回答
1

Microsoft word 似乎可以很好地处理这个问题,就像您单击一个按钮并且操作需要很长时间一样,几秒钟后您会看到一个“工作......”对话框。操作仍然是同步的,您不能中断操作。但是,如果相同的操作发生得很快,您显然不会得到对话框。

如果这是您想要的行为...

您可以通过在 BackgroundWorker 周围包装一个类来相当容易地处理这个问题。只是 DoWork 事件开始的时间,以及第一个进度报告的时间。如果经过了一定的时间,您可以显示您的对话框 - 否则,阻止(因为它是一个简短的过程)。

话虽如此,任何时候你在做可以异步处理的工作,我都建议你这样做。永远不要在明显的时间间隔内阻塞你的 UI 会好得多,即使它很短。通过使用任务并行库,这在 .NET 4(或带有 Rx 框架的 3.5)中变得更加简单。

于 2010-02-15T19:07:05.810 回答
0

Ideally you should be running any IO or non-UI processing either in a background thread or asynchronously to avoid locking up the UI.

于 2010-02-15T19:01:31.670 回答