怎么在一个窗体中使用异步get http资源,等到完成后才可以关闭这个winfrom

20004 2011-01-13 12:07:46
1. winfrom 使用异步 get http
2. 大量数据访问完后,这个winfrom才可以被用户关闭
...全文
77 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
whowhen21 2011-01-13
  • 打赏
  • 举报
回复
如果任务没有完成,则提示,不能关闭。
private bool OK = false;

private void HttpGet() {
WebClient wc = new WebClient();
wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wc_DownloadDataCompleted);
wc.DownloadDataAsync(new Uri("http://www.baidu.com"));
}

void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
//当然,也可以在这里做返回的处理
OK = true;
}

//页面的关闭事件
private void Form1_Closing(object sender, CancelEventArgs e)
{
if(!OK)
MessageBox.Show("任务还没有完成,不能关闭程序!");
e.Cancel = !OK;
}
*************************************************
本内容使用CSDN小秘书回复
每天回帖即可得10分可用分!
*************************************************
宝_爸 2011-01-13
  • 打赏
  • 举报
回复
将下面判断textbox1.text和messagebox的代码换成你检测任务是否完成的代码。


private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Determine if text has changed in the textbox by comparing to original text.
if (textBox1.Text != strMyOriginalText)
{
// Display a MsgBox asking the user to save changes or abort.
if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Cancel the Closing event from closing the form.
e.Cancel = true;
// Call method to save file...
}
}
}


代码来自msdn:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing.aspx

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧