invokerequired

Cleaning up code littered with InvokeRequired

a 夏天 提交于 2019-12-17 15:17:59
问题 I know that when manipulating UI controls from any non-UI thread, you must marshal your calls to the UI thread to avoid issues. The general consensus is that you should use test InvokeRequired, and if true, use .Invoke to perform the marshaling. This leads to a lot of code that looks like this: private void UpdateSummary(string text) { if (this.InvokeRequired) { this.Invoke(new Action(() => UpdateSummary(text))); } else { summary.Text = text; } } My question is this: can I leave out the

Isn't blindly using InvokeRequired just bad practice?

♀尐吖头ヾ 提交于 2019-12-12 10:28:51
问题 I am a novice programmer so I could be completely mistaken here, but this issue bugs me more then it should. This is actually a follow-up from this question. The accepted answer was, that you have to call InvokeRequired in order to avoid some overhead, because there is a chance you are already operating on the UI thread. In theory, I agree that it could save some time. After some tests I found out that using Invoke takes about twice the time compared to calling an operation normally (tests

Invoking a control from backgroundworker stops with no error

匆匆过客 提交于 2019-12-11 23:06:32
问题 I'm running a BackgroundWorker which suppose to update my UserControl . I tried Invoking after checking InvokeRequired property: private void bgwHighlightText_DoWork(object sender, DoWorkEventArgs e) { tmpRich.SelectedRtf = myRtf; if (_ucResultRich.InvokeRequired && _ucResultRich.rchResult.InvokeRequired) _ucResultRich.Invoke(new Action(() => _ucResultRich.rchResult.Rtf = tmpRich.Rtf)); // Debug pointer stops here //Rest of the code } I also tried to invoke the RichTextBox inside my

InvokeRequired doubt

喜夏-厌秋 提交于 2019-12-11 10:49:32
问题 The following method will be invoked from a non UI thread. Should I check InvokeRequired, for calling these items in the method? a. this._moduleStatusGrid.Invalidate() b. this.Close() private void CheckIfAllModulesInitComplete() { this._moduleStatusGrid.Invalidate(); if (this._moduleDataList.Count(moduleData => !moduleData.IsInitOver) == 0) { this._footprint.DeActivate(); this.Close(); } } 回答1: Control.Invoke and Control.BeginInvoke are safe to call from the UI thread and non-UI threads, so

C# InvokeRequired with property getter

你说的曾经没有我的故事 提交于 2019-12-11 04:06:18
问题 I would like to make my getter thread safe. When I do this, I get an error: public ApplicationViewModel SelectedApplication { get { if (InvokeRequired) { BeginInvoke((Action<ApplicationViewModel>)SelectedApplication); } return _applicationsCombobox.SelectedItem as ApplicationViewModel; } } I have the error: Cannot cast expression of type 'Foo.Model.ApplicationViewModel' to type 'Action<ApplicationViewModel>' 回答1: Lots of things wrong: you cannot use BeginInvoke, Invoke is required you cannot

VB.net Invoke a property change in a control

橙三吉。 提交于 2019-12-11 00:38:50
问题 Lots of examples of how to invoke methods, but how does one change a simple property? For demonstration-sake, here's a very simple set of code that should help. Let's say I need to set the visible property from a child form, and thus, it needs to be invoked: Friend Sub activateItem(ByVal myItem As PictureBox) If myItem.InvokeRequired = True Then ???? Else myItem.Visible = True End If End Sub Thanks 回答1: If you're using VB.Net 2010, you can use a lambda expression: If myItem.InvokeRequired

Winforms data-binding to business objects in a multi-threaded scenario without InvokeRequired?

我只是一个虾纸丫 提交于 2019-12-04 05:23:46
问题 For example, I've got a business object Person : class Person : INotifyPropertyChanged { string Name { get; set; } DateTime DateOfBirth { get; set; } } // ^ abbreviated for better legibility; implementation would be trivial And I've got some Winforms UI controls data-bound to an object of this class: Person somePerson = ...; nameTextBox.DataBindings.Add("Text", somePerson, "Name"); dobDatePicker.DataBindings.Add("Value", somePerson, "DateOfBirth"); Now I am making changes to somePerson and

How to Invoke() when I have no Control available

半世苍凉 提交于 2019-12-02 08:08:46
问题 I'm writing a connection handler (a dialog to request username and password). The code is a handler that shows a dialog. This code could be called from a thread, so I need to Invoke() if InvokeRequired . In a ideal situation I can initialize then handler with a Control to do InvokeRequired , but sometimes the Control could be null. Is it possible? How could I implement the code? Is the following correct? public class GuiCredentialsHandler { // control used to invoke if needed private static

InvokeRequired of Form == false and InvokeRequired of contained control == true

北慕城南 提交于 2019-12-01 04:35:39
how is it possible? I have windows Form control, derived from System.Windows.Forms.Form with WebBrowser control contained in this form. Webbrowser object instance is created in constructor of form (in InitializeComponent() method). Then in background thread I manipulate with content of WebBrowser, and I found that in some cases Form.InvokeRequired == false, while WebBrowser.InvokeRequired == true. How can it be? Form.InvokeRequired returns false before the form is shown. I did a simple test: Form2 f2 = new Form2(); Thread t = new Thread(new ThreadStart(() => PrintInvokeRequired(f2))); t.Start(

Run code on UI thread without control object present

戏子无情 提交于 2019-11-28 07:01:40
I currently trying to write a component where some parts of it should run on the UI thread (explanation would be to long). So the easiest way would be to pass a control to it, and use InvokeRequired/Invoke on it. But I don't think that it is a good design to pass a control reference to a "data/background"-component, so I'm searching for a way to run code on the UI thread without the need of having a control available. Something like Application.Dispatcher.Invoke in WPF... any ideas, thx Martin Judah Gabriel Himango There's a better, more abstract way to do this that works on both WinForms and