invoke

Using C# MethodInvoker.Invoke() for a GUI app… is this good?

浪尽此生 提交于 2019-11-27 06:41:39
Using C# 2.0 and the MethodInvoker delegate, I have a GUI application receiving some event from either the GUI thread or from a worker thread. I use the following pattern for handling the event in the form: private void SomeEventHandler(object sender, EventArgs e) { MethodInvoker method = delegate { uiSomeTextBox.Text = "some text"; }; if (InvokeRequired) BeginInvoke(method); else method.Invoke(); } By using this pattern I do not duplicate the actual UI code but what I'm not sure about is if this method is good. In particular, the line method.Invoke() does it use another thread for invoking or

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

喜夏-厌秋 提交于 2019-11-27 06:34:44
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, int j) { return i+j; } int main(int argc, char* argv[]) { int i = 4, j = 2; string function = "function_2"; cout << callFunction(i, j, function) << endl; return 0; } This is the basic approach int callFunction(int i, int j, string function) { if(function == "function_1") { return function_1(i, j); } else if(function == "function_2") { return function_2(i, j); }

How does the event dispatch thread work?

淺唱寂寞╮ 提交于 2019-11-27 04:51:22
With the help of people on stackoverflow I was able to get the following working code of a simple GUI countdown (which just displays a window counting down seconds). My main problem with this code is the invokeLater stuff. As far as I understand invokeLater , it sends a task to the event dispatching thread (EDT) and then the EDT executes this task whenever it "can" (whatever that means). Is that right? To my understanding, the code works like this: In the main method we use invokeLater to show the window ( showGUI method). In other words, the code displaying the window will be executed in the

Communication between BroadcastReceiver and Activity - android

大城市里の小女人 提交于 2019-11-27 03:59:55
问题 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 therefore available, but something tells me it is a very bad idea. Accordingly, I tried to instantiate the broadcast receiver inside my activity without declaring it in my manifest but the problem is - when the app is off, the activity is not exist and

Run code on UI thread without control object present

你说的曾经没有我的故事 提交于 2019-11-27 01:40:27
问题 I currently trying to write a component where some parts of it should run on the UI thread (explanation would be to long). So the easiest way would be to pass a control to it, and use InvokeRequired/Invoke on it. But I don't think that it is a good design to pass a control reference to a "data/background"-component, so I'm searching for a way to run code on the UI thread without the need of having a control available. Something like Application.Dispatcher.Invoke in WPF... any ideas, thx

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

前提是你 提交于 2019-11-27 01:19:33
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 following: [DllImport("Whisper.dll", EntryPoint="Exist", CallingConvention=CallingConvention.Cdecl)] public static extern bool Exist(string name); Yet, whenever I call my function it ALWAYS returns true, even when I commented out my little function and made it return false. I have the feeling there is something wrong with my calling convention or any other

How do I invoke a private static method using reflection (Java)?

六眼飞鱼酱① 提交于 2019-11-27 00:55:00
I would like to invoke a private static method. I have its name. I've heard it can be done using Java reflection mechanism. How can I do it? EDIT: One problem I encountered when trying to invoke the method is how to specify the type of its argument. My method receives one argument and its type is Map. Therefore I cannot do Map<User, String>.TYPE (In run time there's no such a thing as Map because of Java Type erasure). Is there another way to get the method? Let's say you want to call MyClass.myMethod(int x); Method m = MyClass.class.getDeclaredMethod("myMethod", Integer.TYPE); m.setAccessible

Using the C# Dispatcher

别等时光非礼了梦想. 提交于 2019-11-27 00:28:36
I'm building a chat client and am not 100% sure on how to use the dispatcher . So the question is I have a method as such: public void LostConnection() { myGUI.chatBox.AppendText("Lost connection to room: "+ myGUI.UsernameText.ToString() + "\r\n"); } Do i need to surrond the statement within (myGUI.chatBox... ) with a Dispatcher.Invoke ? I appreciate any help. Ricibob Your app has a main UI thread (usually ManagedThreadId==1 ). Typically in a chat app your events will come in on other threads (either dedicated socket listen threads or thread pool threads from listening code). If you want to

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

别等时光非礼了梦想. 提交于 2019-11-26 22:39:27
问题 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) { if (varControl.InvokeRequired) { varControl.BeginInvoke(new MethodInvoker(() => readControlText(varControl))); } else { string varText = varControl.Text; return varText; } } Edit: I guess having BeginInvoke is not nessecary in this case as i need

Control.Invoke is hanging

此生再无相见时 提交于 2019-11-26 22:01:36
问题 I've seen a ton of post regarding the problem of control.Invoke hanging applications but they mostly seemed to be restricted to those running on .NET 1.1. I've also seen suggestions to use .BeginInvoke instead which I have tried but to no avail. .BeginInvoke doesn't hang but it also doesn't call the delegate. My main concern is that I have used .Invoke for years with no issues and I have a major application in the field that uses it extensively and I'm worried that this problem will crop up