开发者

how to handle QUEUE of threads c#

hi i want to build a simple program which does

  Parallel.ForEach(m_CustomerArr, customer =>
        {
            customer.OrderSomthing();
        });

when customer order something he query database, when his item run out of he contact to the supplier

      public bool takeOrder(Order o)
    {
            lock (s_QueueLocker)
            {
                OrderQueue.Enqueue(o);
            }
            //waits here to be signaled by the working thread of supplier 
            o.m_ManualResetEvent.WaitOne();**
        return true;
    }  

there is only one supplier who holdes a queue who have a supplierthread:

   public void CreateThead()
    {
        Order currentOrder;
        while (true)
        {
            if (OrderQueue.Count != 0)
            {
                currentOrder = OrderQueue.Dequeue();
                DoOrder(currentOrder); 
            }
        }

    }
     private void DoOrder(Order currentOrder)
    {
        //update the db this function runes only in one thread 
        currentOrder.m_ManualResetEvent.Set();
        System.Threading.Thread.Sleep(5000);
    }

where m_ManualResetEvent is a member of customer whom is passed to every order in c'tor

   public class Order
{
    public ManualResetEvent m_ManualResetEvent;
    public string m_Query;
    public int m_Quntity;

    pu开发者_C百科blic Order(ManualResetEvent customerManualResetEvent, string query, int quntity)
    {
        m_ManualResetEvent = customerManualResetEvent;
        m_Query = query;
        m_Quntity = quntity;
    }

}

what would be a the best way to stop the thread on wait (in **) and signal him like mutex in c ? I am new to c# multi-threading and my concept is from Linux kernel so things might be easier in C# in a class somebody already built whom implement it...

so my question is ' is there a better design , better practice , better way to do the above ?


If you are really just writing data to the database, you should see whether it's possible to write it in the thread itself and not on a separate thread. If that is even remotely possible, that will save you a lot of trouble.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜