Questions tagged [invoke]

Executes the specified delegate on the thread that owns the control's underlying window handle.

The Invoke method searches up the control's parent chain until it finds a control or form that has a window handle if the current control's underlying window handle does not exist yet. If no appropriate handle can be found, the Invoke method will throw an exception.

1794 questions
64
votes
6 answers

Invoking methods with optional parameters through reflection

I've run into another problem using C# 4.0 with optional parameters. How do I invoke a function (or rather a constructor, I have the ConstructorInfo object) for which I know it doesn't require any parameters? Here is the code I use…
Alxandr
  • 12,345
  • 10
  • 59
  • 95
52
votes
7 answers

How to get return value when BeginInvoke/Invoke is called in C#

I've this little method which is supposed to be thread safe. Everything works till i want it to have return value instead of void. How do i get the return value when BeginInvoke is called? public static string readControlText(Control varControl)…
MadBoy
  • 10,824
  • 24
  • 95
  • 156
49
votes
8 answers

Cleaning up code littered with InvokeRequired

I know that when manipulating UI controls from any non-UI thread, you must marshal your calls to the UI thread to avoid issues. The general consensus is that you should use test InvokeRequired, and if true, use .Invoke to perform the…
Erik Forbes
  • 35,357
  • 27
  • 98
  • 122
42
votes
1 answer

Dispatcher Invoke(...) vs BeginInvoke(...) confusion

I'm confused why I can't make this test counter application work with 2 (or more) simultaneous running countertextboxes with the use of "BeginInvoke" on my Dispatcher in the Count() method. You can solve the issue by replacing the BeginInvoke by an…
Jerev
  • 431
  • 1
  • 4
  • 6
40
votes
6 answers

What's wrong with calling Invoke, regardless of InvokeRequired?

I've seen the common setup for cross threading access to a GUI control, such as discussed here: Shortest way to write a thread-safe access method to a windows forms control All the web hits I found describe a similar thing. However, why do we need…
MattH
  • 4,166
  • 2
  • 29
  • 33
38
votes
5 answers

Best Way to Invoke Any Cross-Threaded Code?

I know that this question has been asked before, but I'm looking for a way to: streamline the creation of safe cross-threaded code. reuse this code in any situation (no Windows Forms references). Here's what I have so far, but I want to remove the…
CLaRGe
  • 1,821
  • 1
  • 25
  • 22
38
votes
1 answer

Ensuring that things run on the UI thread in WPF

I'm building a WPF application. I'm doing some async communication with the server side, and I use event aggregation with Prism on the client. Both these things results in new threads to be spawned which are not the UI thread. If I attempt to do…
stiank81
  • 25,418
  • 43
  • 131
  • 202
38
votes
5 answers

How to call a function by its name (std::string) in C++?

I wonder if there is a simple way to call a function from a string. I know a simple way, using 'if' and 'else'. int function_1(int i, int j) { return i*j; } int function_2(int i, int j) { return i/j; } ... ... ... int function_N(int i,…
Alan Valejo
  • 1,305
  • 3
  • 24
  • 44
36
votes
2 answers

Reflection MethodInfo.Invoke() catch exceptions from inside the method

I have a call to MethodInfo.Invoke() to execute a function through reflection. The call is wrapped in a try/catch block but it still won't catch the exception thrown by the function I'm invoking. I receive the following message: Exception was…
Rodniko
  • 4,926
  • 20
  • 69
  • 93
34
votes
8 answers

C# DllImport with C++ boolean function not returning correctly

I have the following function in a C++ DLL extern "C" __declspec(dllexport) bool Exist(const char* name) { //if (g_Queues.find(name) != g_Queues.end()) // return true; //else // return false; return false; } Inside my C# class I have the…
Shammah
  • 619
  • 1
  • 5
  • 12
34
votes
5 answers

How to return a value with Dispatcher.Invoke?

Anyone knows how to return a value from Dispatcher.Invoke in wpf? I want to return the selected index for a ComboBox. Thanks!
toni
  • 381
  • 1
  • 3
  • 4
33
votes
3 answers

Is it appropriate to extend Control to provide consistently safe Invoke/BeginInvoke functionality?

In the course of my maintenance for an older application that badly violated the cross-thread update rules in winforms, I created the following extension method as a way to quickly fix illegal calls when I've discovered them: /// ///…
Greg D
  • 43,259
  • 14
  • 84
  • 117
31
votes
3 answers

Communication between BroadcastReceiver and Activity - android

I have a broadcast receiver in my app which is fired every time the user gets an incoming call. Now, when it happens, I need the broadcast receiver to invoke a specific method in a specific activity. Now, I tried to make this method static and…
David Lasry
  • 1,407
  • 4
  • 26
  • 43
30
votes
4 answers

How to read combobox from a thread other than the thread it was created on?

I am trying to read a combobox.Text from a thread other than the thread it was created on but I am getting the error: An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll Additional information:…
nitrkli
  • 369
  • 1
  • 4
  • 7
29
votes
3 answers

Fabric vs Invoke

I have been using fabric and recently got told about invoke, so I took a look at it's documentation: Like Ruby’s Rake tool and Invoke’s own predecessor Fabric 1.x, it provides a clean, high level API for running shell commands and …
James Lin
  • 25,028
  • 36
  • 133
  • 233