如何显示请等待图像,而响应正在从服务器返回

本文关键字:响应 返回 服务器 图像 何显示 显示 等待 | 更新日期: 2023-09-27 18:18:28

我正在使用一个基于Windows的应用程序,我从另一个服务器得到响应。

它占用了太多的时间,所以我想显示请等待用户的图像,直到它完成。

我该怎么做?

private void btnSubmit_Click(object sender, EventArgs e)
{  
    WebRequest request = null;
    HttpWebResponse response = null;
    Stream stream = null;
    StreamReader reader = null;
    string url = txtURL.Text.ToString();
    if (url != "")
    {
        try
        {
            // Display Please Wait Image
            Application.Run(new Form1());
            string NavigateURL = "http://" + url + Properties.Settings.Default.portAppName;
            request = HttpWebRequest.Create(NavigateURL + "connectionParam/PostCon");
            request.Method = "POST";
            request.ContentType = "text/xml";
            response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                // My stuff
            }
        }
        catch(Exception ex)
        { }
    }
}

如何显示请等待图像,而响应正在从服务器返回

                BackgroundWorker bw;
    public Form1()
    {
        InitializeComponent();
        label1.Text = "Yes";
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        bw = new BackgroundWorker();
        bw.WorkerReportsProgress = true;
        bw.DoWork += BwOnDoWork;
        bw.WorkerSupportsCancellation = true;
        bw.RunWorkerAsync();
    }
    private void btnSubmit_Click(object sender, EventArgs e)
    {
        // Display Please Wait Image
        label1.Text = "No";
    }
    private void BwOnDoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        while (!worker.CancellationPending)
        {
            if (label1.Text == "No")
            {
                WebRequest request = null;
                HttpWebResponse response = null;
                Stream stream = null;
                StreamReader reader = null;
                string url = txtURL.Text.ToString();
                if (url != "")
                {
                    try
                    {
                        Application.Run(new Form1());
                        string NavigateURL = "http://" + url + Properties.Settings.Default.portAppName;
                        request = HttpWebRequest.Create(NavigateURL + "connectionParam/PostCon");
                        request.Method = "POST";
                        request.ContentType = "text/xml";
                        response = (HttpWebResponse)request.GetResponse();
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            // My stuff
                            worker.CancelAsync();
                            bw.CancelAsync();
                        }
                    }
                    catch (Exception ex) { }
                }
            }
        }
    }

看起来表单中包含您等待的图像是Form1。简单地显示和处理表单可以完成的工作:

private void btnSubmit_Click(object sender, EventArgs e)
{  
    WebRequest request = null;
    HttpWebResponse response = null;
    Stream stream = null;
    StreamReader reader = null;
    string url = txtURL.Text.ToString();
    var form = new Form1();
    if (url != "")
    {
        try
        {
            // Display Please Wait Image
            form.Show();
            string NavigateURL = "http://" + url + Properties.Settings.Default.portAppName;
            request = HttpWebRequest.Create(NavigateURL + "connectionParam/PostCon");
            request.Method = "POST";
            request.ContentType = "text/xml";
            response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                // My stuff
            }
        }
        catch(Exception ex)
        { }
        finally { form.Dispose(); }
    }
}