加载HttpWebRequest时等待指示符

时间:2012-06-05 06:12:45

标签: wpf httpwebresponse

这是我的部分代码

public MainWindow()
        {
            InitializeComponent();
try
                {       
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.site.my/install.php?data=" + uniqueID + "&pass=" + pass);
                    request.BeginGetResponse(new AsyncCallback(FinishWebRequest), request);                   
                }
                catch (Exception)
                {
                }
}
private void FinishWebRequest(IAsyncResult result)
        {
            HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Cleanup the streams and the response.
            reader.Close();
            dataStream.Close();
            response.Close();

            if (responseFromServer == "1")
            {
                MessageBox.show("ok")
            }else{
                MessageBox.show("no")
            }

        }

虽然我使用异步,但似乎窗口会“暂停”一段时间..所以我认为加载指示器会很好,对吧?我应该把它放在哪里?我只想到一个简单的加载指示器,而不是进度条..

2 个答案:

答案 0 :(得分:0)

首先,将所有这些代码放在构造函数中是不好的做法。加载事件是这个逻辑的更合适的地方。其次,你的问题很模糊:你有什么样的装载指标?你想自己写,还是得到第三方?您究竟想要完成什么,GUI的设计是什么?

答案 1 :(得分:0)

我使用此处提供的代码http://elegantcode.com/2009/07/03/wpf-multithreading-using-the-backgroundworker-and-reporting-the-progress-to-the-ui/来执行后台工作程序,应用程序将不再挂起