我有一个问题,我想等到Main()
完成Download()
。但是,文件下载/检查开始,同时其他行开始执行。
我怎样才能使用await
或其他任何东西来等待Main
?
private void Main()
{
Download("http://webserver/file.xml");
//Do something here ONLY if the file exists!!
}
//This method invokes the URL validation
private void Download(downloadURL)
{
System.Uri targetUri = new System.Uri(downloadURL);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);
request.BeginGetResponse(new AsyncCallback(WebRequestCallBack), request);
}
//In this method the URL is being checked for its validity
void WebRequestCallBack(IAsyncResult result)
{
HttpWebRequest resultInfo = (HttpWebRequest)result.AsyncState;
HttpWebResponse response;
string statusCode;
try
{
response = (HttpWebResponse)resultInfo.EndGetResponse(result);
statusCode = response.StatusCode.ToString();
}
catch (WebException e)
{
statusCode = e.Message;
}
onCompletion(statusCode);
}
//This method does not help! I just added if it could be any useful
private void onCompletion(string status)
{
if (status == HttpStatusCode.OK.ToString())
MessageBox.Show("file exists");
else
MessageBox.Show("file does not exists");
}
我需要的是,详细...
- 从给定的 URL 下载文件
- 下载前验证网址
- 如果(已验证)则
- 继续下载并执行其他任务
- 别的
- 失败并停止进程,不要下载!并给出 URL 已损坏(无法验证)的消息!
我正在尝试执行“验证”部分,检查 URL 是否正确并等待响应。我需要某种验证过程的状态才能继续。