Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a clear example demonstrating what I've stated on my comment above. It uses lock instead of Mutex but you'll get the point. It's a simple code using multi-threaded Job producers all adding informations to the same (locked) resource and one consumer running once every second to interact with a form control -ListBox in this case- and clear the jobs cache.</p> <p>You'll find further informations here <a href="http://www.albahari.com/threading/part2.aspx" rel="nofollow">http://www.albahari.com/threading/part2.aspx</a></p> <pre><code>public partial class Form1 : Form { static readonly Queue&lt;Job&gt; queue = new Queue&lt;Job&gt;(); public Form1() { InitializeComponent(); //starts the timer to run the ProcessJobs() method every second System.Threading.Timer t = new System.Threading.Timer(obj =&gt; { ProcessJobs(); }, null, 5000, 1000); } /// &lt;summary&gt; /// Called by informations producers to add jobs to the common queue /// &lt;/summary&gt; private void AddJob(Job job) { lock (queue) { queue.Enqueue(job); } } /// &lt;summary&gt; /// Common queue processing by the consumer /// &lt;/summary&gt; private void ProcessJobs() { Job[] jobs; lock (queue) { jobs = queue.ToArray(); queue.Clear(); } foreach(Job job in jobs){ this.Invoke(new Action(delegate { listBox1.Items.Add(job.Name); })); } } /// &lt;summary&gt; /// Producer /// &lt;/summary&gt; private void JobsProducer(string name) { for (int i = 0; i &lt; 10; i++) { Random r = new Random(); System.Threading.Thread.Sleep(r.Next(1,10)*50); this.AddJob(new Job(string.Format("Job({0}) n.{1}", name, i))); } } /// &lt;summary&gt; /// Starts the jobs producers /// &lt;/summary&gt; private void button1_Click(object sender, EventArgs e) { for (int i = 0; i &lt; 10; i++) { string producerName = string.Format("Producer{0}", i); new System.Threading.Timer(obj =&gt; { JobsProducer(producerName); }, null, 0, System.Threading.Timeout.Infinite); } } } public class Job { //whatever -informations you need to exchange between producer and consumer private string name; public string Name { get { return name; } } public Job(string name) { this.name = name; } } </code></pre> <p>And here you can find an example using the Dictionary to hold several jobs results:</p> <pre><code>Dictionary&lt;string, string[]&gt; jobs = new Dictionary&lt;string, string[]&gt;(); //adds an array to the dictionary //NB: (array it's not well suited if you don't know the values or the size in advance...you should use a List&lt;string&gt;) jobs.Add("jobNumber1", new string[]{"a","b"}); //gets an array from the dictionary string[] jobNumber1; if (!jobs.TryGetValue("jobNumber1", out jobNumber1)) throw new ApplicationException("Couldn't find the specified job in the dictionary"); </code></pre>
    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.
 

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