简体   繁体   中英

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. 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. But instead it is just handled by the regular "chickenStore" and not by the 5 threads. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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