Note that there are some explanatory texts on larger screens.

plurals
  1. POAbout threading in .NET
    text
    copied!<p>Below is a code sample presenting the design of a Windows form I use in a Windows CE application.</p> <p>We are having some unresolved problems in our application, and I suspect that the problem comes from the thread used here as background worker (since the class <code>BackgroundWorker</code> is not available in Windows CE).</p> <p>You can see that a lock object is used to prevent multiple instances of <code>MyWorker</code>.</p> <p>Is it the right way to prevent multiple instances of such "worker"? Would it work as expected? Would a singleton worker be better?</p> <pre><code>public class MainForm : Form { private object myLock = new Object(); private bool isWorkerStarted = false; private Thread worker; public MainForm() { } public void btn_Click() { lock(myLock) { if(!isWorkerStarted) { MyWorker worker = new MyWorker(); worker.StartEvent = new EventHandler(ThreadStart); worker.EndEvent = new EventHandler(ThreadStop); workerThread = new Thread(worker.DoWork); workerThread.Start(); isWorkerStarted = true; } } } public void ThreadStart(object sender, EventArgs args) { lock(myLock) { isWorkerStarted = true; // Invoke some delegate to interact with the window } } public void ThreadStop(object sender, EventArgs args) { lock(mylock) { isWorkerThread = false; } this.Invoke(new NewFormDelegate(OpenForm)); } private void OpenForm() { AnotherWindowForm awf = new AnotherWindowForm(); awf.Show(); this.Close(); } //****************** // Worker class //****************** public class MyWorker() { public event EventHandler StartEvent; public void OnStart() { if(StartEvent != null) { StartEvent(this, new EventArgs()); } } // Edit 2011-07-19 public void OnEnd() { if(EndEvent != null) { EndEvent(this, new EventArgs()); } } public void DoWork() { OnStart(); // Do some work. OnEnd(); } } </code></pre> <p>}</p> <p><strong>Edit 2011-07-19</strong> Here are more details about the goal of the project.</p> <p>We have a Windows form with only one button on a Windows CE device. When the user clicks on the button, a worker thread must be started requesting a WCF service. During the execution of the thread, the little hourglass indicates to the user that the program is busy. When the worker is done (i.e. when he receives a "good" answer), it must stops and open another Windows form on the top of the current one (for that purpose I'm using the method <code>Invoke</code> to delegate the creation of the new form).</p> <p>The only requirement is that the user should <strong>not be able to run two workers</strong> (for example by clicking two times on the button). And the facts are that sometimes, we can see that two workers are making requests to the WCF service from the same device.</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