Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Most properties on WinForm object instances need to be accessed from the thread that they were created on. You can use the Control.InvokeRequired property to determine if you need to use the control (or form) Invoke method to execute the code on the UI thread.</p> <p>It is also a good practise to create most WinForm controls on the main UI thread, and not on any thread pool threads. In WinForms applications, you can use the <code>SynchronizationContext</code> to ensure some code, such as creating a form, is called on the UI thread.</p> <p><strong>EDIT:</strong> changed so that the method doesn't return after movement detected.</p> <pre><code>public void timer1_Tick(object source, EventArgs e) { // assume this is being called on the UI thread, and save the thread synchronization context var uiContext = SynchronizationContext.Current; Parallel.For(0, nbre, l =&gt; { while (true) { Thread.Sleep(250); // &lt;--- sleep for 250 ms to avoid "busy" wait cameraInstanceList[l].Start(); if (cameraInstanceList[l].MoveDetection == true) { // capture instances used in closures below var cameraInstance = cameraInstanceList[l]; var ipAdresse = adresseIPArray[l]; //show the the form S once foreach (Form S in Application.OpenForms) { var frm = S as Formes.CameraViewVS; if (frm != null) { // create delegate to be invoked on form's UI thread. var action = new Action(() =&gt; { if (frm.IP == cameraInstance.adresse) { cameraInstance.MoveDetection = false; frm.WindowState = FormWindowState.Normal; frm.Activate(); } }; if (frm.InvokeRequired) frm.Invoke(action); else action(); continue; // &lt;--- go back to the top of the while loop // and wait for next detection } } // create delegate to create new form on UI thread. var createNewFormCallback = new SendOrPostCallback((o) =&gt; { f1 = new Formes.CameraViewVS(cameraInstance, ipAdresse); f1.Show(this); }; // and invoke the delegate on the ui thread uiContext.Send(createNewFormCallback, null); } } } ); } </code></pre>
 

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