Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>+1 to Nenad and Bastiaan for pointing me in the <code>Control.Invoke</code> direction.</p> <p>In order to fully reap the benefits of the «passive view» pattern, I don't want any knowledge of the <code>Control</code> type of WinForms in the «<a href="http://martinfowler.com/eaaDev/SupervisingPresenter.html" rel="nofollow noreferrer">supervising controller</a>» (that type should only be known by the implementer of the view interface, i.e. the one derived from <code>Form</code>).</p> <p>Here's how I solved the issue satisfactory:</p> <ul> <li>The controller creates a new <code>Thread</code> instance with the <code>ForEach</code> loop a an argument, and then starts the created instance.</li> </ul> <p>Before:</p> <pre><code>View.DateSpan.Workdays.ForEach(d =&gt; { // do stuff... } ); </code></pre> <p>After:</p> <pre><code>new Thread( () =&gt; View.DateSpan.Workdays.ForEach( d =&gt; { // do stuff... } ) ).Start(); </code></pre> <ul> <li>The view's widget update methods use a helper method that checks if the request comes from another thread, and if so uses <code>Invoke</code>. See the code below.</li> </ul> <p>Before: </p> <pre><code>public string Status { set { _statusLabel.Text = value ); } } </code></pre> <p>After:</p> <pre><code>public string Status { set { ExecuteOnUIThread( _statusLabel, () =&gt; _statusLabel.Text = value ); } } </code></pre> <p>The helper method:</p> <pre><code>private static void ExecuteOnUIThread( Control control, Action action ) { if ( control.InvokeRequired ) { control.Invoke( action ); } else { action(); } } </code></pre> <p>The production WinForm view works like a charm, and with the addition of a while loop that spins the thread while the background thread does its work in my BDD stories, so do they with my old view «<a href="http://xunitpatterns.com/Test%20Spy.html" rel="nofollow noreferrer">test spy</a>».</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload