c# - Control.BeginInvoke() not working without MethodInvoker - But why? -


Ich has a small WinForms program, which has 1 button and 1 textbox if I click on the button then 1 Count 100,000 programmers and shows in milliseconds in the text box in each stage in the current time. Cowhopping elimination is running in a separate thread.

  public partial category Form 1: form {public representative zero myDelegate (); Public myDelegate mydelegate; Public Form 1 () {Initialization (); Mydelegate = new myDelegate (b); } Private Zero Button 1_Click (Object Sender, EventArgs e) {button2.Focus (); Thread t = new thread (a); T.Start (); } Private Zero Form 1_CADDOWN (Object Sender, KEventErgus E) {console.lightline (E. ccode); } Public Zero A (for (int i = 0; i & lt; 100000; i ++) {textbox1.BeginInvoke (mydelegate);}} Public Zero B () {textBox1.Text = GetCurrentMilli (.) ToString (); textBox1.Refresh ();} Public Static Double GetCurrentMilli () {DateTime Jan 1970 = New DateTime (1970, 1, 1, 0, 0, 0, DateTime.UTC); TimeSpace Javascript = DateTime.Utakano - January 1970; Return Java Aspen Milliseconds;}}   

If I run it, then the program works, but the loop is getting free until the loop is over. But why? I called Invooke?!

if I change

  textBox1.BeginInvoke (mydelegate); with   

  textBox1.Invoke (new method ad (b));   

So it works without freeze or problems but why? < / Div>

When you call BeginInvoke you are scheduling a UI update, and then after that Continue with your program without waiting for that UI update. When you do this, some times are cured, but the problem you are having is that you are sending 100,000 requests in one go, and to get it through all those requests, Time will be taken, and nothing else can be done in that time because any new UI updates go to the end of the line, and will not be displayed until the other requests end.

There are ways to keep your general perspective and try to cut the other actions in front of the line, the proper approach is to remove the problem in the first place. No need to send 100,000 updates.

If you want the text box to be the clock's presence in which it ticks, then a timer will be a good tool for the job; You can handle the tick event to update the text box every second, quarter second, or some other "human time" interval.

If this idea is to update the UI, the progress of some long-running operations, you want to make sure that you do not update progress so often. For example, instead of each one , Update every dozen iterations of your loop.

Comments

Popular posts from this blog

c++ - ERROR: cannot open source file x11\xlib.h -

c - What are pthread cancelation points used for? -

c# - Show a huge number of data in DevExpress GridView -