简体   繁体   English

WPF调度程序问题

[英]WPF Dispatcher issue

I'm attempting to update my app to use WPF instead of WinForms and have a problem I need a few pointers as I can't figure out what is wrong.... 我正在尝试更新我的应用程序以使用WPF而不是WinForms,但遇到一个问题,我需要一些指针,因为我无法弄清楚出了什么问题。

The code below is from my Winform app which works fine 下面的代码来自我的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);
    }
}

The following are changes I've made in an attempt to make it work in WPF but the app crashes on the line if (!this.Dispatcher.CheckAccess()) 以下是我为使其在WPF中工作而进行的更改,但如果(!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);
    }
}

with stack trace.... 与堆栈跟踪...。

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)

Can someone help me get the correct syntax to replace this.InvokeRequired and this.Invoke in WPF or point out something obvious I'm missing? 有人可以帮助我获取正确的语法来替换WPF中的this.InvokeRequired和this.Invoke或指出我显然缺少的东西吗?

Thanks O 谢谢O

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

should be : 应该 :

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

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM