WPF Dispatcher问题

时间:2012-08-16 12:53:37

标签: c# wpf c#-4.0 asynchronous

我正在尝试更新我的应用程序以使用WPF而不是WinForms并遇到问题我需要一些指示,因为我无法弄清楚出了什么问题....

以下代码来自我的Winform应用程序,可以正常使用

private delegate void LoginAsyncCallbackDelegate(IAsyncResult result);
private void LoginAsyncCallback(IAsyncResult result)
{
    if (this.InvokeRequired)
    {
        try
        {
            LoginAsyncCallbackDelegate d = new LoginAsyncCallbackDelegate(LoginAsyncCallback);
            this.Invoke(d, new object[] { result });
        }
        catch (Exception ex)
        {
            AddErrorToList(ex.ToString());
        }
    }
    else
    {
        DRVis.upx.api.global.LoginResp resp = APIWrapper.UPGlobalService.Endlogin(result);

        var state = (CustomAsyncStateContainer)result.AsyncState;

        // Session manager
        if (resp != null && resp.header != null && resp.header.sessionToken != null)
            SessionTokenManager.ReturnSessionToken(resp.header.sessionToken);

        DisplayLogin(resp, state);
    }
}

以下是我为尝试使其在WPF中运行所做的更改,但应用程序崩溃 if(!this.Dispatcher.CheckAccess())

private delegate void LoginAsyncCallbackDelegate(IAsyncResult result);
private void LoginAsyncCallback(IAsyncResult result)
{
    if (!this.Dispatcher.CheckAccess()) //Progam Crashes here!!
    {
        try
        {
            LoginAsyncCallbackDelegate d = new LoginAsyncCallbackDelegate(LoginAsyncCallback);
            this.Dispatcher.Invoke(DispatcherPriority.Normal, d, new object[] { result });
        }
        catch (Exception ex)
        {
            AddErrorToList(ex.ToString());
        }
    }
    else
    {
        DRVis.upx.api.global.LoginResp resp = APIWrapper.UPGlobalService.Endlogin(result);

        var state = (CustomAsyncStateContainer)result.AsyncState;

        // Session manager
        if (resp != null && resp.header != null && resp.header.sessionToken != null)
            SessionTokenManager.ReturnSessionToken(resp.header.sessionToken);

        DisplayLogin(resp, state);
    }
}

堆栈跟踪....

The error time: 16/08/2012 13:06
Exception: System.ArgumentException: Object of type 'System.Object[]' cannot be converted to type 

'System.IAsyncResult'.
   at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
   at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, 

CultureInfo culture, Signature sig)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, 

Object[] parameters, CultureInfo culture)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 

numArgs, Delegate catchHandler)

有人可以帮助我在WPF中使用正确的语法来替换this.InvokeRequired和this.Invoke,或指出一些我不知道的明显内容吗?

由于 0

1 个答案:

答案 0 :(得分:1)

this.Dispatcher.Invoke(DispatcherPriority.Normal, d, new object[] { result });

应该是:

this.Dispatcher.Invoke(DispatcherPriority.Normal, d, result);