无响应窗口
My code calls a Web service method which takes a few minutes to perform the operation.
During that time my window becomes non responsive and it shows a complete white screen.
I don't want to the call method from a different thread.
It it the best way to handle it?
Environment: C#, web service
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
BackgroundWorker 是您的朋友。
这是一个 示例,说明我如何将 BackgroundWorker 与 WebService 结合使用。基本上,如果不使用单独的线程,就无法在 UI 端执行密集型操作。 BackgroundWorker 是在单独线程上运行的最佳方式。
The BackgroundWorker is your friend.
Here's an example of how I use a BackgroundWorker with a WebService. Basically, there's no way to do intensive operations on the UI side without using a separate thread. The BackgroundWorker is the nicest way of running on a separate thread.
要拥有响应式 UI,您必须使用另一个线程。
但如果您使用 Visual Studio,生成的客户端类具有异步方法签名,可以为您完成此操作。如果你的方法是
“GetData”,那么您应该有一个名为“GetDataAsync”的方法,它不会冻结您的窗口。
这是一个例子:
To have a responsive UI, you must use another thread.
But if you use visual studio, the generated client class have asynchronous method signatures wich would do it for you. If your method is
"GetData", then you should have a method called "GetDataAsync" wich would not freeze your window.
Here is an example :
您可以在单独的线程上发出请求,这将使 UI 线程保持响应。完成后,您需要将响应同步回 UI 线程。
You can make the request on a separate thread, which will leave the UI thread responsive. You'll need to synchronise the response back to the UI thread once you've finished.