У меня проблема, я хочу подождать в 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
- if (verfied) then
- continue downloading and do other tasks
- else
- fail and stop the process, Don't download! and give a message that URL was broken (couldn't verified)!
Я пытаюсь выполнить часть "Проверка", проверяя правильность URL-адреса и ожидая ответа. Мне нужен какой-то СТАТУС процесса проверки, чтобы продолжить.
//Do something here ONLY if the file exists!!
нельзя было сделать вWebRequestCallBack
илиonCompletion
? - person JosephHirn   schedule 28.01.2013onCompletion
нелегко. Мне нужна непрерывность кода. - person wafers   schedule 28.01.2013