Is it possible to reload a stream using ISynchronizeInvoke.BeginInvoke ()?

My problem is this:

I have two threads, my user interface thread and a workflow. My workflow runs in a separate class, which receives an instance of the form, which passes itself as ISynchronizeInvokea work class, which then uses Invokeon this interface to call its events, which provide status updates for the display user interface. It works great.

I noticed that my background thread seemed to work slowly, so I changed the call to Invoketo BeginInvoke, believing that "I just provide progress updates, it should not be exactly synchronous, there was no harm," except that now I getting weird with updating progress. My progress bar is updated, but the shortcut text does not appear, and if I go to another window and try to change it, it acts just like the UI thread is blocked, so I wonder if it is possible that my progress calls (which happens very often ) overload the user interface thread so much that they never process messages. Is this possible at all, or is there something else here?

+3
source share
2 answers

You are finally overloading the user interface thread.

In your first example, you were (behind the scenes) sending a message to the UI thread, waiting for it to be processed (which is the invoke target, which ultimately depends on SendMessage ), and then send another. At the same time, other messages were probably in the queue (WM_PAINT messages, for example) and processed.

, BeginInvoke ( PostMessage), , . , , (WM_PAINT ..), ""

, ; .

, , this - .

+3

;

  • ; , ; , , 50/500. , Invoke/BeginInvoke
  • ; BeginInvoke , ...
  • - , ; ( ) , BeginEdit/EndEdit, , ; , End*

... ():

List<string> stuff = new List<string>();
for(int i = 0 ; i < 50000 ; i++) {
    stuff.Add(i.ToString());
    if((i % 100) == 0) {
        // update UI
        BeginInvoke((MethodInvoker) delegate {
            foreach(string s in stuff) {
                listBox.Items.Add(s);
            }
        });
    }
}

, - stuff? , ( BeginInvoke) . . ( ), . :

  • Invoke
  • , ( , , )
+1

Source: https://habr.com/ru/post/1716654/


All Articles