Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have to use the <strong>Control.InvokeRequired</strong> property to determine if you are on a background thread. Then you need to invoke your logic that modified your UI via the <strong>Control.Invoke</strong> method to force your UI operations to occur on the main thread. You do this by creating a delegate and passing it to the <strong>Control.Invoke</strong> method. The catch here is you need some object derived from <strong>Control</strong> to call these methods.</p> <p><strong>Edit</strong>: As another user posted, if yo you can wait to the <strong>BackgroundWorker.Completed</strong> event to update your UI then you can subscribe to that event and call your UI code directly. BackgroundWorker_Completed is called on the main app thread. my code assumes you want to do updates during the operation. One alternative to my method is to subscribe to the <strong>BwackgroundWorker.ProgressChanged</strong> event, but I believe you'll need to still call <strong>Invoke</strong> to update your UI in that case.</p> <p>for example</p> <pre><code>public class UpdateController { private UserController _userController; BackgroundWorker backgroundWorker = new BackgroundWorker(); public UpdateController(LoginController loginController, UserController userController) { _userController = userController; loginController.LoginEvent += Update; } public void Update() { // The while loop was unecessary here backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork); backgroundWorker.RunWorkerAsync(); } public delegate void DoUIWorkHandler(); public void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { // You must check here if your are executing on a background thread. // UI operations are only allowed on the main application thread if (someControlOnMyForm.InvokeRequired) { // This is how you force your logic to be called on the main // application thread someControlOnMyForm.Invoke(new DoUIWorkHandler(_userController.UpdateUsersOnMap); } else { _userController.UpdateUsersOnMap() } } } </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