System.Net.WebClient 问题
我正在编写一个必须访问 MySQL 数据库的程序。为此,我编写了一个 PHP WebService 来获取表中的所有值。我遇到的问题是我正在用 C# 编写一个函数来获取这些值。我希望该函数等待,直到调用 DownloadStringCompleted 事件的事件处理程序,然后返回值。我是这样做的:
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadCompletedHandler);
client.DownloadStringAsync(new Uri(Application.Current.Host.Source.AbsoluteUri + "\\PHP\\GetAdmins.php"));
while (DownloadCompleted == false) { }
return DownloadResult;
但这会使程序挂起。
我需要一些东西来使程序的该部分暂停,直到 DownloadConpleted = true。
我正在运行 Visual Studio Ultimate 2010,我正在制作 Silverlight 应用程序,如果有任何帮助,我们将不胜感激。
I am writing a program that has to access a MySQL database. To do this I wrote a PHP WebService to get all the values in a table. The problem I am having is that I am writing a function in C# to get those values. I want the function to wait until a event handler on the DownloadStringCompleted event is called, then return the values. I did it like this:
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadCompletedHandler);
client.DownloadStringAsync(new Uri(Application.Current.Host.Source.AbsoluteUri + "\\PHP\\GetAdmins.php"));
while (DownloadCompleted == false) { }
return DownloadResult;
But that makes the program hang.
I need something that will make that part of the program pause until DownloadConpleted = true.
I am running Visual Studio Ultimate 2010, I am making a Silverlight application, and any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
ManualResetEvent
- 从下载完成处理程序发出完成事件信号并让主线程等待它:这回答了你的问题,但它仍然会让 UI 线程等待 - 你真的必须接受 Silverlight 的异步,所以努力寻找一个解决方案在
DownloadStringCompleted
事件处理程序中更新您的模型 - 这样您就不必使用拐杖,并且您的应用程序将执行得更好。You could use a
ManualResetEvent
- signaling the completion event from the download completion handler and making the main thread wait on it:This answers your question, but it still will make the UI thread wait - you really have to embrace async with Silverlight, so work towards a solution that updates your model in the
DownloadStringCompleted
event handler - that way you don't have to use crutches and your application will perform much better.您可以使用 ManualResetEvent:
请注意,阻止主线程线程是一种不好的做法,因为它会冻结 UI。更好的方法是简单地在下载完成事件处理程序中处理结果。由于此处理程序在与 UI 线程不同的线程上执行,因此不要忘记将更新 UI 的任何调用封送到主线程。
推荐示例:
这样您就不再阻塞主线程,并且 UI 保持流畅。异步操作完成后,您可以通过使用 Dispatcher.BeginInvoke 方法。
You could use a ManualResetEvent:
Note that blocking the main thread is a bad practice because it will freeze the UI. A better way would be to simply handle the results in the download completed event handler. And because this handler is executed on a thread different than the UI thread don't forget to marshal any calls that update the UI to the main thread.
Recommended example:
This way you are no longer blocking the main thread and the UI remains fluid. Once the asynchronous operation completes you update the UI with the result by marshalling the call to the UI thread using the Dispatcher.BeginInvoke method.