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

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

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

tech