invoke

Is this a proper way of calling synchronous methods asynchronously?

微笑、不失礼 提交于 2019-12-10 21:15:47
问题 I was using code like this: handler.Invoke(sender, e); But the problem with that code is that it is synchronous and all it really does is update the GUI. It is not necessary for the server to wait for it to complete so it should be made asynchronous. I don't really want to use BeginInvoke and EndInvoke because a callback method is not necessary in this case. Is this a suitable alternative? Task.Factory.StartNew(() => handler.Invoke(sender, e)); 回答1: The way you did it is fine, another way to

Is there any difference between using Invoke for the parent form or for the target control?

女生的网名这么多〃 提交于 2019-12-10 15:21:04
问题 What's the difference between check InvokeRequired and call Invoke method for a control and for the parent form if I want to deal with the control from another thread? if (theForm.InvokeRequired) Invoke(...) Or if (myControl.InvokeRequired) myControl.Invoke(...) From MSDN: 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. 回答1: These two have the same effect.

Avoiding `ObjectDisposedException` while calling `Invoke`

天涯浪子 提交于 2019-12-10 14:28:07
问题 I have 2 forms, one is MainForm and second is DebugForm . The MainForm has a button that sets up and shows the DebugForm like this, And passes a reference to an already opened SerialPort: private DebugForm DebugForm; //Field private void menuToolsDebugger_Click(object sender, EventArgs e) { if (DebugForm != null) { DebugForm.BringToFront(); return; } DebugForm = new DebugForm(Connection); DebugForm.Closed += delegate { WindowState = FormWindowState.Normal; DebugForm = null; }; DebugForm.Show(

C# Catching Exception From Invoked Delegate on another Thread

旧街凉风 提交于 2019-12-10 11:22:47
问题 I have some code as follows. This is running on "Thread 2" WebBrowser browser = this.webBrowser browser.Invoke(new MethodInvoker(delegate { browser.Document.GetElementById("somebutton").InvokeMember("click"); })); Thread.Sleep(500); browser.Invoke(new MethodInvoker(delegate { browser.Document.GetElementById("username").SetAttribute("value", username); })); //folowed by several more similar statments Essentially I am Invoking some methods on a WebBrowser control created on a different thread,

C#/WinForms: ShowDialog and subsequent Show on Form

不打扰是莪最后的温柔 提交于 2019-12-10 11:22:00
问题 For reasons I think are not relevant here I have one or more Threads that comunicate with a single instance UI (a Form). In the worker threads I need to report progress or input data or simple choices. All of those come from user interaction with the UI and, of course, for M$ .NET, all the UI runs in the main thread. Obviously I need to handle the thread synchronization across the UI (main) thread and the worker ones. I do it by properly verifying InvokeRequired and company. There is a miriad

VB.NET Invoke cannot be called on a control until the window handle has been created, BUT the handle is created

只愿长相守 提交于 2019-12-10 10:14:01
问题 This is my situation, there are 2 Classes and my main form Form1: Class1: has a method doSomethingAndCall(callback) which creates a new thread Class2: has dynamic created controls with a button that fires Class1.doSomethingAndCall(newCallback) in code it looks like this (it starts at Class2.Button_Click): Class Class1 public shared sub doSomethingAndCallAsync(state as object) Console.WriteLine(Form1.InvokeRequired) 'output: false Console.WriteLine(Form1.IsHandleCreated) 'output: false Form1

javascript setTimeout call error

ⅰ亾dé卋堺 提交于 2019-12-09 11:39:48
问题 I want to invoke the window.setTimeot function with my custom scope so I use the call method, but there is something wrong. function foo() { this.bar = function() { console.log("keep going"); window.setTimeout.call(this,this.bar,100); } this.bar(); } new foo; under Firefox this prints to the console only 1 line and then nothing, and under google chrome it throws a TypeError. What is the problem in my code? 回答1: Using call does not help here: it calls setTimeout with your this object, but the

Exception has been thrown by the target of an invocation (MethodBase.Invoke Method)

允我心安 提交于 2019-12-09 06:49:40
问题 I want to catch the exceptions that are thrown in methods called with invoke method. public void TestMethod() { try { method.Invoke(commandHandler, new[] { newCommand }); } catch(Exception e) { ExceptionService.SendException(e); } } method.Invoke calls the following method: public void Register(/*parameters*/) { if(test_condition()) throw new CustomException("Exception Message"); } The problem is that when I catch the CustomException, in the TestMethod, the e variable on the catch statement

How to access a field's value via reflection (Scala 2.8)

依然范特西╮ 提交于 2019-12-09 05:19:00
问题 Consider the following code: class Foo(var name: String = "bar") Now i try to get the value and the correct type of it via reflection: val foo = new Foo val field = foo.getClass.getDeclaredField("name") field.setAccessible(true) //This is where it doesn't work val value = field.get(????) I tried things like field.get(foo), but that just returns an java.lang.Object but no String. Basically I need the correct type, because I want to invoke a method on it (e. g. toCharArray). What is the

Getting a return value from a methodInfo.invoke

会有一股神秘感。 提交于 2019-12-08 14:47:01
问题 How do I get a return value (int) from a methodInfo.invoke ? What makes it difficult for me is the fact that I use a string variable to call the method. Check the example below: if (Convert.ToBoolean(getParameterFromXML("issue", k, 1)) == true) { m = k + 1; MethodInfo methodInfo = typeof(frmDetails).GetMethod("Issue" + m); methodInfo.Invoke(this, Parameters); } What can I do? Any help would be appreciated. 回答1: When I read this you get the result of the method back from the Invoke-call. It is