In .NET, what thread will Events be handled in?

后端 未结 4 963
既然无缘
既然无缘 2020-12-08 13:17

I have attempted to implement a producer/consumer pattern in c#. I have a consumer thread that monitors a shared queue, and a producer thread that places items onto the sha

4条回答
  •  攒了一身酷
    2020-12-08 13:42

    you have to use autoresetevent handlers for this problem.....in autoresetevent when producer produses it set the signal then consumer reset its signal and consume.. after consuming consume set signal then only producer produced...

    AutoResetEvent pro = new AutoResetEvent(false);
    AutoResetEvent con = new AutoResetEvent(true);
    
    public void produser()
    {
    
        while(true)
        {
            con.WaitOne();
    
            pro.Set();
        }
    }
    
    public void consumer()
    {
        while (true)
        {
        pro.WaitOne();
           .................****
    
        con.Set();
        }
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        Thread th1 = new Thread(produser);
        th1.Start();
        Thread th2 = new Thread(consumer);
        th2.Start();
    }
    

提交回复
热议问题