invoke

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

青春壹個敷衍的年華 提交于 2019-11-28 06:13:04
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'? 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

How to instantiate the class in an assembly using Reflection with C#/.NET?

拈花ヽ惹草 提交于 2019-11-28 06:04:57
问题 I have this library to compiled to calc.dll. namespace MyClass { public class Calculator { public int Value1 {get; set;} public int Value2 {get; set;} public Calculator() { Value1 = 100; Value2 = 200; } public int Add(int val1, int val2) { Value1 = val1; Value2 = val2; return Value1 + Value2; } } } I want to instantiate the Calculate class without linking to the calc.dll. Can C# do that? I came up with this code, but I don't know how to instantiate the Calculator class. using System; using

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

拈花ヽ惹草 提交于 2019-11-28 04:48:53
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 instance of my base class and call the method I need. Eventually that will happen, but right now,

Control.Invoke is hanging

扶醉桌前 提交于 2019-11-28 01:41:41
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 there. I am doing nothing differently as far as I can tell between my working code and the code that

Unable to access UNC Paths in Powershell remote session

社会主义新天地 提交于 2019-11-28 01:07:57
I am unable to access the UNC paths on my servers in a Powershell remote session from my local machine. I am able to use them from Servers Cmd prompt directly. Actually, I have logged into the server and mapped a UNC path as local drive (say X:). Used Reconnect on Login option. I have a batch file which resides in this X: drive, I want to run it remotely using invoke command from my local script. But, it fails. It says "Cannot find drive. A drive with name X doesn't exist". Also, when I try to map drive using net use command in the scriptblock then also it throws an error - System error 1223 -

Invoke method by MethodInfo

我的未来我决定 提交于 2019-11-28 00:39:52
问题 I want to invoke methods with a certain attribute. So I'm cycling through all the assemblies and all methods to find the methods with my attribute. Works fine, but how do I invoke a certain method when I only got it's MethodInfo. AppDomain app = AppDomain.CurrentDomain; Assembly[] ass = app.GetAssemblies(); Type[] types; foreach (Assembly a in ass) { types = a.GetTypes(); foreach (Type t in types) { MethodInfo[] methods = t.GetMethods(); foreach (MethodInfo method in methods) { // Invoke a

Cross-thread exception when setting WinForms.Form owner - how to do it right?

有些话、适合烂在心里 提交于 2019-11-27 18:27:36
问题 I have a main UI thread which runs the application and creates the main window form (let's call it W ). I have also a secondary thread that I spin up and which creates a dialog box (let's call it B ). I want to set the owner of the dialog B to be the main window W . The setting of B s owner happens on the thread that created B . Basically: b.Owner = w; but this throws a cross-thread exception telling me that I am tryng to access the W object from the wrong thread. So I tried to execute the

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

我只是一个虾纸丫 提交于 2019-11-27 18:08:05
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 value from GUI before the thread can continue. So using Invoke is good as well. Just no clue how to use

MethodInfo.Invoke performance issue

时间秒杀一切 提交于 2019-11-27 17:29:13
问题 I am reading and writing data to and from a file. The data in the file can be floats, doubles, ints etc. The type is not known until runtime. I will refer to data type stored in the file as Tin. Data is read into or written from arrays of type Tout. This type too is not known until runtime. The code sequence is something like this. In the Open method Tin and Tout are known, we can create read and write methods for the known data types. Open(...) { MethodInfo ReadMethod = typeof(...)GetMethod(

Cleaning up code littered with InvokeRequired

随声附和 提交于 2019-11-27 17:23:35
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 InvokeRequired test and just call Invoke, like so: private void UpdateSummary(string text) { this.Invoke(new