invoke

C# Return value from function invoked in thread

谁都会走 提交于 2019-12-17 21:36:21
问题 I have a calculating thread function which invokes message function from other thread using Invoke and I want that calculating thread to get value(of valuetype, like integer) from that message function. How can I do this? The problem is that I still get old value of x variable after Invoke(...) and I expect value of 15 delegate void mes_del(object param); void MyThreadFunc() { ... int x = 5; object [] parms = new object []{x}; Invoke(new mes_del(MessageFunc), (object)parms); ... } void

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

那年仲夏 提交于 2019-12-17 18:00:55
问题 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 to check InvokeRequired? Can't we just call Invoke directly? I assume the answer is no, so my real question is 'why'? 回答1: 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

How to use Reflection to Invoke an Overloaded Method in .NET

纵饮孤独 提交于 2019-12-17 17:42:38
问题 Is there a way to Invoke an overloaded method using reflection in .NET (2.0). I have an application that dynamically instantiates classes that have been derived from a common base class. For compatibility purposes, this base class contains 2 methods of the same name, one with parameters, and one without. I need to call the parameterless method via the Invoke method. Right now, all I get is an error telling me that I'm trying to call an ambiguous method. Yes, I could just cast the object as an

Cleaning up code littered with InvokeRequired

僤鯓⒐⒋嵵緔 提交于 2019-12-17 15:22:31
问题 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 marshaling. This leads to a lot of code that looks like this: private void UpdateSummary(string text) { if (this.InvokeRequired) { this.Invoke(new Action(() => UpdateSummary(text))); } else { summary.Text = text; } } My question is this: can I leave out the

Cleaning up code littered with InvokeRequired

a 夏天 提交于 2019-12-17 15:17:59
问题 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 marshaling. This leads to a lot of code that looks like this: private void UpdateSummary(string text) { if (this.InvokeRequired) { this.Invoke(new Action(() => UpdateSummary(text))); } else { summary.Text = text; } } My question is this: can I leave out the

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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 08:32:49
问题 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

Invoking methods with optional parameters through reflection

放肆的年华 提交于 2019-12-17 04:59:49
问题 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 now: type.GetParameterlessConstructor() .Invoke(BindingFlags.OptionalParamBinding | BindingFlags.InvokeMethod | BindingFlags.CreateInstance, null, new object[0], CultureInfo.InvariantCulture); (I've just tried with different BindingFlags ).

Find the smallest element in an array. [closed]

怎甘沉沦 提交于 2019-12-13 09:23:29
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago . I think the problem lies with the invoking of the method or the braces, not 100% sure. When I call the method does it matter if it before or after the main method? public class varb { public static void main (String[] args) { double[] array = new double [10]; java.util.Scanner input = new java.util

How do i dynamically invoke a dll method that receives a callback as parameter?

此生再无相见时 提交于 2019-12-13 07:14:55
问题 Overview: I am writting an application to dynamically load .dlls and call their methods. Since the .dlls are doing heavy i/o in background, i've made callbacks to notify the UI about what's happening "down there" Pieces of Code: dllName = (string) e.Argument; // Assembling Complete path for the .dll file completePath = Path.Combine(ConfigurationManager.AppSettings["DllsFolder"], dllName); Assembly assembler = Assembly.LoadFrom (completePath); // Creating Instance of Crawler Object

Threads totally freezes the GUI

点点圈 提交于 2019-12-13 06:43:50
问题 private delegate void Runner(); //A delegate, but this attempt didn't work public void Run() { Stager.InstructionsMemory = InstructionsTextBox.Text.Split('\n'); Stager.InintializeLists(); InstructionThread = new Thread[Stager.InstructionsMemory.Length]; PipelineInitializor(); BlockingCollection<Func<object>>[] tasks = new BlockingCollection<Func<object>>[Stager.InstructionsMemory.Length]; PCounterLabelUpdate up = new PCounterLabelUpdate(PCounterLabelUpdater); MemoryUpdater MemUp = new