Search code examples
c#multithreadingconcurrent-queue

Multiple class instances raising same event


I'm having trouble figuring out what is wrong with my C# code.

I'm trying to learn how to use ConcurrentQueue class in System.Collections.Concurrent namespace.

In order to do this, I'm creating 2 instances of the same class in different threads, passing to the constructors a different Listbox control.

I am expecting each class instance of EventGenerator to raise events at random intervals, updating the Listbox the were passed with randomly generated number, and adding that number to a ConcurrentQueue which is also passed to the constructor.

In my main thread, is the method to DeQueue the ConcurrentQueue of objects EnQueued to it by both spawned threads.

But what I'm getting is the 2 EnQueue Listboxes displaying the same data and the DeQueue Listbox seeming reporting to have deQueued them both.

I apologize if my description is not good enough, and my code follows, along with a link to an image of my form in case it might better help visualize what I'm trying to do...

Form

public partial class Form1 : Form
{
    ConcurrentQueue<int> CQ;
    EventGenerator eventGenerator1;
    EventGenerator eventGenerator2;
    public Form1()
    {
        InitializeComponent();
        CQ = new ConcurrentQueue<int>();
        eventGenerator1 = new EventGenerator(CQ, listBox1);
        eventGenerator1.OnRandomEvent += new EventGenerator.RandomEventHandler(RandomEvent);
        eventGenerator2 = new EventGenerator(CQ, listBox2);
        eventGenerator2.OnRandomEvent += new EventGenerator.RandomEventHandler(RandomEvent);
    }

    private void RandomEvent(object sender, IncomingConnectionEventArgs e)
    {
        string s = e.Property_Int.ToString()
            + " "
            + e.Property_String;
        UpdateListbox(s, e.LB);
    }

    private void UpdateListbox(string argstring, ListBox argListBox)
    {
        if (InvokeRequired)
        {
            Invoke(new Action<string, ListBox>(UpdateListbox), new object[] { argstring, argListBox });
            return;
        }
        int n;
        bool b = false;
        //do
        //{
            b = CQ.TryDequeue(out n);
        //} while (!b);

        argListBox.Items.Add(argstring);
        argListBox.SelectedIndex = argListBox.Items.Count -1;
        listBoxDeQueue.Items.Add(n.ToString());
        listBoxDeQueue.SelectedIndex = listBoxDeQueue.Items.Count - 1;

    }

    private void button_Start_Click(object sender, EventArgs e)
    {
        Thread methodThread1 = new Thread(new ThreadStart(TheThread1));
        methodThread1.Start();
        Thread methodThread2 = new Thread(new ThreadStart(TheThread2));
        methodThread2.Start();
    }

    private void TheThread2()
    {
        eventGenerator2.Start();
    }

    private void TheThread1()
    {
        eventGenerator1.Start();

    }

    private void button_Stop_Click(object sender, EventArgs e)
    {
        eventGenerator1.Stop();
        eventGenerator2.Stop();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        eventGenerator1.Stop();
        eventGenerator2.Stop();
    }
}

IncomingConnectionEventArgs

class IncomingConnectionEventArgs : EventArgs
{
    public System.Windows.Forms.ListBox LB;
    public int Property_Int { get; set; }
    public string Property_String { get; set; }

    public IncomingConnectionEventArgs(int argInt, string argString,
        System.Windows.Forms.ListBox lb)
    {
        LB = lb;
        Property_Int = argInt;
        Property_String = argString;
    }
}

EventGenerator

class EventGenerator
{
    public delegate void RandomEventHandler(
        object sender, 
        IncomingConnectionEventArgs e);

    public event RandomEventHandler OnRandomEvent;

    public Random r = new Random();

    public ListBox listBox;

    public bool Generate = true;

    public ConcurrentQueue<int> CQ;

    public EventGenerator(ConcurrentQueue<int> argCQ, ListBox argListBox)
    {
        CQ = argCQ;
        listBox = argListBox;
    }

    public void Start()
    {
        Generate = true;
        while (Generate)
        {
            Thread.Sleep(r.Next(100, 2000));
            RandomEvent();
        }
    }

    public void Stop()
    {
        Generate = false; ;
    }

    public void RandomEvent()
    {
        if (OnRandomEvent == null)
        {
            return;
        }

        int n = r.Next(1000, 10000);
        CQ.Enqueue(n);
        IncomingConnectionEventArgs Args =
            new IncomingConnectionEventArgs(n, "", listBox);

        OnRandomEvent(this, Args);
    }
}

enter image description here


Solution

  • The problem is with your use of Random. Unless you use a single instance of Random or explicitly seed each instance differently, the two threads calling Random.Next(...) will typically generate the same values.

    A better way to seed your instance is this:

    Random r = new Random(Guid.NewGuid().GetHashCode());