invoke

BackgroundWorker still needs to call Invoke?

[亡魂溺海] 提交于 2019-12-01 08:27:38
问题 In the last question Display progress bar while doing some work in C#?, people has recommend use of BackgroundWorker . I thought in BackgroundWorker DoWork method you can update the GUI directly, but why this function call need to be called using Invoke . toolTip.SetToolTip(button, toolTipText); 回答1: The RunWorkerCompleted callback is marshalled onto the UI thread; DoWork is not. You should use the ReportProgress method to update the UI during DoWork processing. See: How to: Run an Operation

Control.Invoke unwraps the outer exception and propagates the inner exception instead

只谈情不闲聊 提交于 2019-12-01 05:17:57
The MessageBox.Show call below shows "Inner". Is this a bug? private void Throw() { Invoke(new Action(() => { throw new Exception("Outer", new Exception("Inner")); })); } private void button1_Click(object sender, EventArgs e) { try { Throw(); } catch (Exception ex) { MessageBox.Show(ex.Message); // Shows "Inner" } } I had a look at the reference source for System.Windows.Forms.Control , and the code that deals with Invoke looks like this: try { InvokeMarshaledCallback(current); } catch (Exception t) { current.exception = t.GetBaseException(); } GetBaseException : public virtual Exception

Control.Invoke unwraps the outer exception and propagates the inner exception instead

筅森魡賤 提交于 2019-12-01 02:06:51
问题 The MessageBox.Show call below shows "Inner". Is this a bug? private void Throw() { Invoke(new Action(() => { throw new Exception("Outer", new Exception("Inner")); })); } private void button1_Click(object sender, EventArgs e) { try { Throw(); } catch (Exception ex) { MessageBox.Show(ex.Message); // Shows "Inner" } } 回答1: I had a look at the reference source for System.Windows.Forms.Control , and the code that deals with Invoke looks like this: try { InvokeMarshaledCallback(current); } catch

Calling ASP.net Web Service from C# Application

你说的曾经没有我的故事 提交于 2019-11-30 18:48:59
I have a question. How can i invoke a web service and get the result from a C# desktop application. I am making a desktop app and I want it to be able to connect to my online ASP.net web services. How is this possible? In Solution Explorer, right-click your project node and select Add Service Reference. Enter the URL where your service WSDL is located. This is usually the URL of the service itself. This generates a strongly-typed proxy class in a new Services References folder in your project. Write code in your desktop app to instantiate the proxy class and invoke methods on it. The rest

Parameter count mismatch with Invoke?

时光怂恿深爱的人放手 提交于 2019-11-30 17:31:52
The code block below results in the error: TargetParameterCountException was unhandled by user code. Parameter count mismatch. public void AddListViewItem(string[] Data) { if (InvokeRequired) { Invoke(new Action<string[]>(AddListViewItem), Data); } else { ListViewData.Items.Add(Data[0]).SubItems.AddRange ( new string[] { Data[1], Data[2], Data[3], } ); } } Any ideas? The error occurs because of array covariance; an array of strings is assignable to object[] . This causes the Invoke method to treat each element of the string array as if it should be an argument to the AddListViewItem method.

Help with understanding C# syntax while Invoking a new Action

早过忘川 提交于 2019-11-30 15:39:23
问题 I am new to c# and do not understand the syntax of invoking a new action or even what an action is. From my understanding in Port1_DataReceived, I have to create an action because I am in a new tread... Can anyone elaborate on why I need to do this? public Form1() { InitializeComponent(); SerialPort Port1 = new SerialPort("COM11", 57600, Parity.None, 8, StopBits.One); Port1.DataReceived += new SerialDataReceivedEventHandler(Port1_DataReceived); Port1.Open(); } private void Port1_DataReceived

Help with understanding C# syntax while Invoking a new Action

无人久伴 提交于 2019-11-30 15:10:52
I am new to c# and do not understand the syntax of invoking a new action or even what an action is. From my understanding in Port1_DataReceived, I have to create an action because I am in a new tread... Can anyone elaborate on why I need to do this? public Form1() { InitializeComponent(); SerialPort Port1 = new SerialPort("COM11", 57600, Parity.None, 8, StopBits.One); Port1.DataReceived += new SerialDataReceivedEventHandler(Port1_DataReceived); Port1.Open(); } private void Port1_DataReceived(object sender, SerialDataReceivedEventArgs e) { SerialPort Port = (SerialPort)sender; string Line = "";

C# Threading using invoke, freezing the form

徘徊边缘 提交于 2019-11-30 14:19:08
I'm trying to use threads and prevent the program from freezing while the thread is busy. It should show the progress (writing of 0's / 1's) and not just show the result after its done, freezing the form in the meanwhile. In the current program I'm trying to write to a textbox, and actually see constant progress, and the form can't be affected by the tasks of the other thread. What I have now is I can write to a textbox with a thread using invoke, but It only shows the result (Form freezes while thread is busy), and the form freezes. Form image: using System; using System.Collections.Generic;

C# Winforms Threading: Closed Form Gets Invoked

元气小坏坏 提交于 2019-11-30 13:06:02
问题 The following code demonstrates my dilemma. The code creates a background thread which processes something, then Invokes the UI thread with the result. It may throw an exception if the background thread calls Invoke on the form after the form has closed. It checks IsHandleCreated before calling Invoke, but the form might close after the check. void MyMethod() { // Define background thread Action action = new Action( () => { // Process something var data = BackgroundProcess(); // Try to ensure

COM object methods are not executed on the thread that CoInitialize-d and created the object

痞子三分冷 提交于 2019-11-30 10:09:57
I am developing an UI application that creates a COM object along the way. The problem is, I want to "move" this COM object entirely on a different thread. What I do is this: create the new thread I want to move the object into (with CreateThread API) after entering this thread, I'm calling PeekMessage to setup a message queue for it calling CoInitialize, CoCreateInstance to create the COM object, QueryInterface to get the interface I want finally I call a method on the interface that displays a MessageBox with the value returned by GetCurrentThreadId() (I have access to the VB6 code of the