invoke

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

馋奶兔 提交于 2019-11-28 22:20:45
问题 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: Cross-thread operation not valid: Control 'levelsComboBox' accessed from a thread other than the thread it was created on. I have used .Invoke before but only to set properties, how can I use it to read combobox.Text? Because .Invoke returns void and I

Func<T>() vs Func<T>.Invoke()

这一生的挚爱 提交于 2019-11-28 20:59:29
I'm curious about the differences between calling a Func directly vs using Invoke() on it. Is there a difference ? Is the first, syntactical sugar, and calls Invoke() underneath anyway ? public T DoWork<T>(Func<T> method) { return (T)method.Invoke(); } vs public T DoWork<T>(Func<T> method) { return (T)method(); } Or am I on the wrong track entirely :) Thanks. There's no difference at all. The second is just a shorthand for Invoke , provided by the compiler. They compile to the same IL. Invoke works well with new C# 6 null propagation operator, now u can do T result = method?.Invoke(); instead

How to execute a method passed as parameter to function

懵懂的女人 提交于 2019-11-28 19:04:18
I want to write my own function in JavaScript which takes a callback method as a parameter and executes it after the completion, I don't know how to invoke a method in my method which is passed as an argument. Like Reflection. example code function myfunction(param1, callbackfunction) { //do processing here //how to invoke callbackfunction at this point? } //this is the function call to myfunction myfunction("hello", function(){ //call back method implementation here }); You can just call it as a normal function: function myfunction(param1, callbackfunction) { //do processing here

C# Multithreading — Invoke without a Control

荒凉一梦 提交于 2019-11-28 18:49:39
I am only somewhat familiar with multi-threading in that I've read about it but have never used it in practice. I have a project that uses a third party library that shares the status of an input device by raising events. The problem is, the way the library is written these events are raised from a different thread. My application does not need to be multi-threaded and I've run into a lot of classic threading issues (UI controls complaining about being interacted with from a different thread, collections that get modified as one piece of code is iterating over it, etc.). I just want the 3rd

C# Return value from function invoked in thread

老子叫甜甜 提交于 2019-11-28 14:48:35
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 MessageFunc(object result) { int res = 15; (result as object[])[0] = res; } I tried some approaches like

Cross-thread operation not valid in Windows Forms

旧街凉风 提交于 2019-11-28 13:48:28
Could anyone help me i have a problem I'm trying to get this code to work in the background via threadpool but i cannot seem to get it to work i keep getting this error: Cross-thread operation not valid: Control 'ListBox3' accessed from a thread other than the thread it was created on. Here is the code i am using: private void DoWork(object o) { var list = ListBox3; var request = createRequest(TxtServer.Text, WebRequestMethods.Ftp.ListDirectory); using (var response = (FtpWebResponse)request.GetResponse()) { using (var stream = response.GetResponseStream()) { using (var reader = new

Invoking all setters within a class using reflection

耗尽温柔 提交于 2019-11-28 12:45:35
I have a domain object, that for the purposes of this question I will call Person with the following private variables: String name int age Each of these have getters and setters. Now I also have a Map<String, String> with the following entries: name, phil age, 35 I would like to populate a list of all setter methods within the class Person and then looping through this list and invoking each method using the values from the map. Is this even possible as I cannot see any examples close to this on the net. Examples are very much appreciated. Sure it's possible! You can get all methods that

VB.NET Delegates and Invoke - can somebody explain these to me?

断了今生、忘了曾经 提交于 2019-11-28 07:49:39
I'm new to the world of threading, but a few aspects of an app I'm working on require me to use a BackgroundWorker control to prevent the UI freezing up while it's doing some file operations. What I'm trying to do is update a couple of form labels from within the BackgroundWorker. Having never worked with this before I very quickly discovered that I can't access controls that weren't created within the same thread, so after a bit of research I've implemented the following code that seems to make everything work: Private Delegate Sub DelegateUpdateStatus(ByVal statusText As String, ByRef

Run code on UI thread without control object present

戏子无情 提交于 2019-11-28 07:01:40
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 Martin Judah Gabriel Himango There's a better, more abstract way to do this that works on both WinForms and

How to return a value with Dispatcher.Invoke?

删除回忆录丶 提交于 2019-11-28 06:57:35
问题 Anyone knows how to return a value from Dispatcher.Invoke in wpf? I want to return the selected index for a ComboBox. Thanks! 回答1: There's another way that returns value from Invoke(): object oIsLoaded = container.Dispatcher.Invoke( new Func<bool> ( () => { return container.IsLoaded; }) ); And by the way, chances are that the initial code (which is working with delegate) won't modify oIsLoaded at all; So I'd rather use a Func<> for returning a value from that kind of function. 回答2: int result