invoke
Is it appropriate to extend Control to provide consistently safe Invoke/BeginInvoke functionality?
You should create Begin and End extension methods as well. And if you use generics, you can make the call look a little nicer. public static class ControlExtensions { public static void InvokeEx<T>(this T @this, Action<T> action) where T : Control { if (@this.InvokeRequired) { @this.Invoke(action, new object[] { @this }); } else { if … Read more
How to return a value with Dispatcher.Invoke?
There’s another way that returns value from Invoke(): object oIsLoaded = container.Dispatcher.Invoke( new Func<bool> ( () => { return container.IsLoaded; }) ); And by the way, chances are that the initial code (which is working with delegate) won’t modify oIsLoaded at all; So I’d rather use a Func<> for returning a value from that kind … Read more
C# DllImport with C++ boolean function not returning correctly
I found the solution for your problem. Your declaration should be preceded with this marshaling: [return:MarshalAs(UnmanagedType.I1)] so everything should look like this: [DllImport(“Whisper.dll”, EntryPoint=”Exist”, CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool Exist([MarshalAs(UnmanagedType.LPStr)] string name); I tested it in my very simple example and it worked! EDIT Why this happens? C defines bool as 4 bytes int … Read more
Reflection MethodInfo.Invoke() catch exceptions from inside the method
EDIT: As I understand your issue, the problem is purely an IDE one; you don’t like VS treating the exception thrown by the invocation of the MethodInfo as uncaught, when it clearly isn’t. You can read about how to resolve this problem here: Why is TargetInvocationException treated as uncaught by the IDE? It appears to … Read more
Ensuring that things run on the UI thread in WPF
Going over each of your questions, one by one: Not quite; you should only invoke onto the UI thread when necessary. See #2. Yes, it does matter. You should not just automatically Invoke everything. The key is to only invoke onto the UI thread if necessary. To do this, you can use the Dispatcher.CheckAccess method. … Read more
How to call a function by its name (std::string) in C++?
What you have described is called reflection and C++ doesn’t support it. However you might come with some work-around, for example in this very concrete case you might use an std::map that would map names of functions (std::string objects) to function pointers, which in case of functions with the very same prototype could be easier … Read more
Best Way to Invoke Any Cross-Threaded Code?
You also could use an extension method and lambdas to make your code much cleaner. using System.ComponentModel; public static class ISynchronizeInvokeExtensions { public static void InvokeEx<T>(this T @this, Action<T> action) where T : ISynchronizeInvoke { if (@this.InvokeRequired) { @this.Invoke(action, new object[] { @this }); } else { action(@this); } } } So now you can … Read more
What’s wrong with calling Invoke, regardless of InvokeRequired?
From non-UI threads we can’t touch the UI – very bad things can happen, since controls have thread affinity. So from a non-UI thread we must (at a minumum) call Invoke or BeginInvoke. For UI-threads, however – we don’t want to call Invoke lots of time; the issue is that if you are already on … Read more
Dispatcher Invoke(…) vs BeginInvoke(…) confusion
When you use Dispatcher.BeginInvoke it means that it schedules the given action for execution in the UI thread at a later point in time, and then returns control to allow the current thread to continue executing. Invoke blocks the caller until the scheduled action finishes. When you use BeginInvoke your loop is going to run … Read more