begininvoke

Anonymous method as parameter to BeginInvoke?

我的未来我决定 提交于 2019-11-29 06:08:58
问题 Why can't you pass an anonymous method as a parameter to the BeginInvoke method? I have the following code: private delegate void CfgMnMnuDlg(DIServer svr); private void ConfigureMainMenu(DIServer server,) { MenuStrip mnMnu = PresenterView.MainMenu; if (mnMnu.InvokeRequired) { mnMnu.BeginInvoke((CfgMnMnuDlg)ConfigureMainMenu, new object[] { server}); } else { // Do actual work here } } I'm trying to avoid declaring the delegate. Why can't I write something like the below instead? Or can I,

Invoke and BeginInvoke

有些话、适合烂在心里 提交于 2019-11-29 02:21:24
问题 Greetings, I am developing some application in C#. At the moment I'm dealing with threading and I have a question that I have in my mind. What is the difference between Invoke and BeginInvoke? I read some thread and I found some useful information here: here However what is the difference between Invoke and BeginInvoke in the following code: private void ProcessRoutine() { for (int nValue = StartFrom; nValue <= EndTo; nValue++) { this.Invoke(this.MyDelegate, nValue); //this.BeginInvoke(this

How to use BeginInvoke C#

心不动则不痛 提交于 2019-11-28 15:03:26
问题 Could you explain this for me please: someformobj.BeginInvoke((Action)(() => { someformobj.listBox1.SelectedIndex = 0; })); Could you tell me how can I use begininvoke exactly? What is Action type? Why there is blank brackets () ? And what does this mean => ? 回答1: Action is a Type of Delegate provided by the .NET framework. The Action points to a method with no parameters and does not return a value. () => is lambda expression syntax. Lambda expressions are not of Type Delegate . Invoke

beginInvoke, GUI and thread

十年热恋 提交于 2019-11-28 03:56:12
问题 I have application with two thread. One of them (T1) is main GUI form, another (T2) is function working in loop. When T2 gets some information must call function with GUI form. I'm not sure that I do it right. T2 call function FUNCTION, which update something in GUI form. public void f() { // controler.doSomething(); } public void FUNCTION() { MethodInvoker method = delegate { f(); }; if ( InvokeRequired ) { BeginInvoke( method ); } else { f(); } } But now I must declare two function. How

Must every BeginInvoke be followed by an EndInvoke?

我是研究僧i 提交于 2019-11-27 21:09:00
This page in the MS documentation , covering asynchrony in Windows Forms applications, states: You can call EndInvoke to retrieve the return value from the delegate, if neccesary, but this is not required. (emphasis added) This page covering the general case of asynchronous delegates , states something different: No matter which technique you use, always call EndInvoke to complete your asynchronous call. These two seem to be in direct conflict. Which is true? Can someone explain? see also, a post by Phil Haack . Related: Is EndInvoke optional, sort-of optional, definitely not optional? Unless

Dispatcher.BeginInvoke: Cannot convert lambda to System.Delegate

这一生的挚爱 提交于 2019-11-27 18:39:18
I'm trying to call System.Windows.Threading.Dispatcher.BeginInvoke . The signature of the method is this: BeginInvoke(Delegate method, params object[] args) I'm trying to pass it a Lambda instead of having to create a Delegate. _dispatcher.BeginInvoke((sender) => { DoSomething(); }, new object[] { this } ); It's giving me a compiler error saying that I can't convert the lambda to a System.Delegate. The signature of the delegate takes an object as a parameter and returns void. My lambda matches this, yet it's not working. What am I missing? Reed Copsey Since the method takes a System.Delegate ,

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

Dispatcher Invoke(…) vs BeginInvoke(…) confusion

会有一股神秘感。 提交于 2019-11-27 06:54:18
I'm confused why I can't make this test counter application work with 2 (or more) simultaneous running countertextboxes with the use of "BeginInvoke" on my Dispatcher in the Count() method. You can solve the issue by replacing the BeginInvoke by an Invoke. But this doesn't solve my confusion. Here's the sample code I'm talking about: public class CounterTextBox : TextBox { private int _number; public void Start() { (new Action(Count)).BeginInvoke(null, null); } private void Count() { while (true) { if (_number++ > 10000) _number = 0; this.Dispatcher.BeginInvoke(new Action(UpdateText), System

Is Delegate.EndInvoke() really necessary?

大城市里の小女人 提交于 2019-11-27 01:42:43
I've read a couple of forums and even a stackoverflow question or two saying that Delegate.EndInvoke is necessary when using Delegate.BeginInvoke. Many of the articles I've read talking about using BeginInvoke have failed to mention using EndInvoke. Also I've deployed production code using only BeginInvoke and there doesn't appear to be any memory issues. The way I've used BeginInvoke is generally with threads that I don't care about when they finish or how long they take to process. From the MSDN article 'Calling Synchronous Methods Asynchronously' : No matter which technique you use, always

Display progress bar while doing some work in C#?

大城市里の小女人 提交于 2019-11-27 00:03:28
I want to display a progress bar while doing some work, but that would hang the UI and the progress bar won't update. I have a WinForm ProgressForm with a ProgressBar that will continue indefinitely in a marquee fashion. using(ProgressForm p = new ProgressForm(this)) { //Do Some Work } Now there are many ways to solve the issue, like using BeginInvoke , wait for the task to complete and call EndInvoke . Or using the BackgroundWorker or Threads . I am having some issues with the EndInvoke, though that's not the question. The question is which is the best and the simplest way you use to handle