У меня большая проблема при вызове веб-службы из моего приложения WPF. Приложение / окно блокируется до завершения процесса. Я попытался запустить это асинхронно, но проблема все еще сохраняется.
В настоящее время мой веб-сервис может длиться 45-60 секунд. Он запускает процесс на сервере, чтобы получить большой кусок данных. Поскольку мне потребовалось немного времени, я хотел, чтобы индикатор выполнения двигался неопределенно, чтобы пользователь мог видеть, что приложение не остановилось или что-то еще (вы знаете, как они нетерпеливы).
Итак:
private void btnSelect_Click(object sender, RoutedEventArgs e)
{
wDrawingList = new WindowDrawingList(systemManager);
AsyncMethodHandler caller = default(AsyncMethodHandler);
caller = new AsyncMethodHandler(setupDrawingList);
// open new thread with callback method
caller.BeginInvoke((Guid)((Button)sender).Tag, MyAsyncCallback, null);
}
Нажмите кнопку, и приложение создаст форму, в которую будет отправлено асинхронное содержимое, и настроит асинхронное содержимое, вызывающее асинхронный метод.
public bool setupDrawingList(Guid ID)
{
if (systemManager.set(ID))
{
wDrawingList.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() =>
{
wDrawingList.ShowForm();
Hide();
}));
return true;
}
return false;
}
Это асинхронный метод. Метод showForm содержит вызовы для настройки новой формы, включая вызов веб-службы monster
public void MyAsyncCallback(IAsyncResult ar)
{
// Because you passed your original delegate in the asyncState parameter of the Begin call, you can get it back here to complete the call.
MethodDelegate dlgt = (MethodDelegate)ar.AsyncState;
// Complete the call.
bool output = dlgt.EndInvoke(ar);
try
{
// Retrieve the delegate.
AsyncResult result = (AsyncResult)ar;
AsyncMethodHandler caller = (AsyncMethodHandler)result.AsyncDelegate;
// Because this method is running from secondary thread it can never access ui objects because they are created
// on the primary thread.
// Call EndInvoke to retrieve the results.
bool returnValue = caller.EndInvoke(ar);
// Still on secondary thread, must update ui on primary thread
UpdateUI(returnValue == true ? "Success" : "Failed");
}
catch (Exception ex)
{
string exMessage = null;
exMessage = "Error: " + ex.Message;
UpdateUI(exMessage);
}
}
public void UpdateUI(string outputValue)
{
// Get back to primary thread to update ui
UpdateUIHandler uiHandler = new UpdateUIHandler(UpdateUIIndicators);
string results = outputValue;
// Run new thread off Dispatched (primary thread)
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, uiHandler, results);
}
public void UpdateUIIndicators(string outputValue)
{
// update user interface controls from primary UI thread
sbi3.Content = "Processing Completed.";
}
Любая помощь или теории приветствуются. Я в растерянности.
Заранее спасибо