Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It can easily be handled by giving Storage.Number the number -1 when there is no more updates and adding an if statement at printer:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace Threads { class Storage { static int _number; public readonly static object LockNumber = new object(); public static int Number { get { lock (LockNumber) { Monitor.Pulse(LockNumber); return _number; } } set { lock (LockNumber) { _number = value; Monitor.Pulse(LockNumber); Monitor.Wait(LockNumber); } } } } class Counter { public Thread t = new Thread(new ThreadStart(CounterFunction)); public Counter() { t.Start(); } public static void CounterFunction() { for (int i = 0; i &lt; 25; i++) { Storage.Number = i; } Storage.Number = -1; } } class Printer { public Thread t1 = new Thread(new ThreadStart(Print)); public Printer() { t1.Start(); } public static void Print() { Boolean stop = false; while (!stop) { if (Storage.Number != -1) { Console.WriteLine("Number is " + Storage.Number); } else { stop = true; } } } } class Check { static void Main() { Storage s1 = new Storage(); Counter c = new Counter(); Printer p = new Printer(); c.t.Join(); if (!c.t.IsAlive) { p.t1.Abort(); } Thread.Sleep(10000); } } } </code></pre> <p>Though this will still result in some numbers being printed twice. So we're starting the threads from the main class (Check) and use an event handler to stop the threads. Also we'll save the last value in Printer so that none of the values is printed twice (printing is mostly a lot faster then incrementing).</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace Threads { class Storage { static int _number; public readonly static object LockNumber = new object(); public static int Number { get { lock (LockNumber) { Monitor.Pulse(LockNumber); return _number; } } set { lock (LockNumber) { _number = value; Monitor.Pulse(LockNumber); Monitor.Wait(LockNumber); } } } } class Counter { public delegate void Done(); public event Done OnDone; public Counter() { //t.Start(); } public void CounterFunction() { for (int i = 0; i &lt; 25; i++) { Storage.Number = i; } Storage.Number = -1; if (OnDone != null) { OnDone(); } } } class Printer { public Printer() { //t1.Start(); } public void Print() { Boolean stop = false; int prevNumber = -1; while (!stop) { if (Storage.Number != -1) { if (Storage.Number != prevNumber) { prevNumber = Storage.Number; Console.WriteLine("Number is " + Storage.Number); } } else { stop = true; } } } } public partial class Check : Form //Invoking is a System.Windows.Forms function { public Thread _cThread; public Thread _pThread; static void Main() { Check ch = new Check(); } public Check() { Storage s1 = new Storage(); Counter c = new Counter(); c.OnDone += new Counter.Done(countDone); Printer p = new Printer(); _cThread = new Thread(new ThreadStart(c.CounterFunction)); _pThread = new Thread(new ThreadStart(p.Print)); _cThread.Start(); _pThread.Start(); while (true) ; //This is only here so that you can see the results. } private void countDone() { if (_pThread.IsAlive) { _pThread.Abort(); } //Close the threads nicely if (this.InvokeRequired) { this.Invoke(new MethodInvoker(this.countDone)); //This says: invoke and then call countDone. } } } } </code></pre> <p>You will need to reference System.Windows.Forms</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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