简体   繁体   English

C#线程处理事件

[英]C# Threads Handling event

I'm trying to have the threads also handle the pricecutEvent but I cannot get the threads to subscribe to the priceCutEvent. 我正在尝试让线程也处理pricecutEvent,但是我无法让线程订阅priceCutEvent。 So it is not handled by each thread. 因此,它不是由每个线程处理的。 The reason I'm doing this is when a priceCutEvent occurs the threads are supposed to "buy" more chickens. 我这样做的原因是当priceCutEvent发生时,线程应该“购买”更多的鸡。 But instead it is just handled by the regular "chickenStore" and not by the 5 threads. 但是,它仅由常规的“ chickenStore”处理,而不是由5个线程处理。 How do I make it so the threads are actually handling the event? 我如何做到这一点,以便线程实际上在处理事件? I tried: 我试过了:

ChickenFarm.priceCut += new priceCutEvent(reatilers[i].chickenOnSale); 

but that does not work. 但这不起作用。 Removed a lot for you. 为您去除了很多东西。

public delegate void priceCutEvent(Int32 pr); //define a delegate
public delegate void orderEvent();

public static void changePrice(Int32 price)
{
    if (price < chickenPrice) //a price cut occured
    {
        if (priceCut != null) //there is at least one subscriber
            priceCut(price); //emit event to subscriber
    }
    chickenPrice = price;
}
public class myApplication
{
    static void Main(string[] args)
    {
        ChickenFarm chicken = new ChickenFarm();
        multiCellBuffer thisBuffer = new multiCellBuffer();

        /*Alternatively we could use this instead of a regular expression:
        ThreadStart starter = delegate{chicken.farmerFunc(thisBuffer);};
        Thread farmer = new Thread(starter);
        farmer.start(); */
        Thread farmer = new Thread(() => chicken.farmerFunc(thisBuffer));
        farmer.Start();     // Start one farmer thread

        Retailer chickenstore = new Retailer();
        Retailer.orderInNeedOfProcessing += new orderEvent(chicken.processOrder);
        Thread[] retailers = new Thread[5];
        ChickenFarm.priceCut += new priceCutEvent(chickenstore.chickenOnSale);
        for (int i = 0; i < 5; i++) // Start N retailer threads
        {
            //Thread thread = new Thread(() => ReadCentralOutQueue("test"));
            retailers[i] = new Thread(() => chickenstore.retailerFunc(thisBuffer, chicken));
            retailers[i].Name = (i + 1).ToString();         
            retailers[i].Start();     
        }
    }
}
public void chickenOnSale(Int32 p) // Event handler
{
    // order chickens from chicken farm - send order into queue
    OrderObject myOrder = new OrderObject();
    myOrder.setID(Thread.CurrentThread.Name);
    Console.WriteLine("Order ID: {0}", Thread.CurrentThread.Name);

    Int64 myRandomCardNo = rng.Next(1000000000);
    Int32 myRandomAmount = rng.Next(0, 100);
    myOrder.setCardNo(myRandomCardNo);
    myOrder.setAmount(myRandomAmount);

    String myOrderString = encoder(myOrder);
    Console.WriteLine("Un-Encrypted Order: {0}", myOrder.toString()); 
    Console.WriteLine("Encrypted Order: {0}", myOrderString);
    sendOrder(myBuffer, myOrderString);

    Console.WriteLine("Store {0} chickens are on sale: as low as ${1} each",
        Thread.CurrentThread.Name, p);
}

Unfortunately events don't work like that. 不幸的是,事件并非如此。 The handlers are always executed by the thread raising the event, not the thread subscribing to the event. 处理程序始终由引发事件的线程执行,而不是由预订事件的线程执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM