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….
The code below is from my Winform app which works fine
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())
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?
Thanks
O
should be :
?