我正在使用基于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)
{ }
}
}
答案 0 :(得分:0)
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) { }
}
}
}
}
答案 1 :(得分:0)
表单似乎包含您的等待图片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(); }
}
}