Multithreading / C #: can I execute BeginInvoke for multiple user interface elements?

Say I'm on a workflow, and I would like to change a few user interface elements, button, text box, etc.

Do I need to call BeginInvoke for each item? i.e.,

  myButton.BeginInvoke(someMethod);
  myTextBox.BeginInvoke(someOtherMethod);

Or is there a way to make one BeginInvoke and then update multiple user interface elements? Thanks

+3
source share
3 answers

Just call BeginInvoke on the form and update all controls from there.


@James Black: my shortcut should add a method like this:

private IAsyncResult BeginInvoke(MethodInvoker method) {
    return BeginInvoke((Delegate)method);
}

Then name it like this:

BeginInvoke(() => {
    txtName.Text = name;
});
+3
source

, . ( , , ), , BeginInvoke on; , , - , . , , BeginInvoke on.

+4

, :

TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();

, .

a Task :   private void changeElements() {     ( (() = >     {       // ui     .})) Start (uiScheduler);   }

, , BeginInvoke, .

But, as mentioned, you do not need to call several calls BeginInvoketo update user interfaces, since after the first you are already in the main thread.

0
source

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


All Articles