public void RunOnce()
        {
            int         sleep;
            int         invoiceNum;
            int         total     = 0;
            string      polist    = "";
            XmlDocument xdInvoice = new XmlDocument();

            xdInvoice.LoadXml("<Invoice/>");
            XmlElement xeInvoice = xdInvoice.DocumentElement;

            invoiceNum = ms_InvoiceCounter++;
            xeInvoice.SetAttribute("InvoiceID", invoiceNum.ToString());

            Random r = new Random((int)DateTime.Now.Ticks / int.Parse(Thread.CurrentThread.Name));

            sleep = r.Next(Global.InvoiceAppMinSleepTime, Global.InvoiceAppMaxSleepTime);
            Thread.Sleep(sleep);
#if Interceptor
            BAMInterceptor interceptor = Global.LoadInterceptor("BAMApiInvoice_interceptor.bin");
            interceptor.OnStep(Global.dataExtractor, "locNewInvoice", xeInvoice, Global.es);
#else
            Global.es.BeginActivity("BAMApiInvoice", invoiceNum.ToString());
#endif

            lock (PosToInvoice)
            {
                if (PosToInvoice.Count < Global.InvoicePoMinCount)
                {
                    return;
                }

                while (PosToInvoice.Count > 0)
                {
                    XmlElement xePo    = (XmlElement)PosToInvoice.Dequeue();
                    string     poid    = xePo.GetAttribute("PoID");
                    XmlElement xePoRef = xdInvoice.CreateElement("PoRef");
                    xePoRef.SetAttribute("PoID", poid);
                    xeInvoice.AppendChild(xePoRef);

                    int amount = int.Parse(xePo.SelectSingleNode("Price").InnerText);
                    total  += amount;
                    polist += poid + " ";

#if Interceptor
                    interceptor.OnStep(Global.dataExtractor, "locAddPoToInvoice", xeInvoice, Global.es);
#else
                    Global.es.AddRelatedActivity(
                        "BAMApiInvoice", invoiceNum.ToString(),
                        "BAMApiPo", poid.ToString());
#endif
                }
            }
            xeInvoice.SetAttribute("Total", total.ToString());
            Console.WriteLine("Invoice #" + invoiceNum + " send for { " + polist + "}");

#if Interceptor
            interceptor.OnStep(Global.dataExtractor, "locSendInvoice", xeInvoice, Global.es);
#else
            Global.es.UpdateActivity("BAMApiInvoice", invoiceNum.ToString(),
                                     "Send", DateTime.UtcNow,
                                     "Total", xeInvoice.GetAttribute("Total"));
#endif
            // Simulate delay for Invoice
            sleep = r.Next(Global.InvoiceWaitMinTime, Global.InvoiceWaitMaxTime);
            Thread.Sleep(sleep);
            Console.WriteLine("Invoice " + invoiceNum + " was paid");
#if Interceptor
            interceptor.OnStep(Global.dataExtractor, "locInvoicePaid", xeInvoice, Global.es);
#else
            Global.es.UpdateActivity("BAMApiInvoice", invoiceNum.ToString(), "Paid", DateTime.UtcNow);
            Global.es.EndActivity("BAMApiInvoice", invoiceNum.ToString());
#endif
        }
        void RunOnce()
        {
            Random r     = new Random((int)DateTime.Now.Ticks / int.Parse(Thread.CurrentThread.Name));
            int    sleep = r.Next(Global.PoAppMinSleepTime, Global.PoAppMaxSleepTime);

            Thread.Sleep(sleep);

            // Generate Random PurchaseOrder as XML message
            int    poid    = ms_poCounter++;
            string sPOxml  = "<PurchaseOrder PoID=\"" + poid.ToString() + "\">\n";
            int    product = r.Next(Global.Products.Length);

            sPOxml += "    <Product>" + Global.Products[product] + "</Product>\n";
            int discount = r.Next(Global.Discounts.Length);

            sPOxml += "    <Discount>" + Global.Discounts[discount] + "</Discount>\n";
            sPOxml += "    <Price>" + Global.Prices[product, discount] + "</Price>\n";
            sPOxml += "    <Address>" + r.Next(10000).ToString() + " " + r.Next(1000).ToString() + " Str</Address>\n";
            sPOxml += "</PurchaseOrder>\n";

            XmlDocument xdPO = new XmlDocument();

            xdPO.LoadXml(sPOxml);
            XmlElement xePO = xdPO.DocumentElement;

            Console.WriteLine("New Purchase Order #" + xePO.GetAttribute("PoID") + " Received.");

#if Interceptor
            BAMInterceptor interceptor = Global.LoadInterceptor("BAMApiPo_interceptor.bin");
            interceptor.OnStep(Global.dataExtractor, "locNewPo", xePO, Global.es);
#else
            Global.es.BeginActivity("BAMApiPo", poid.ToString());
            Global.es.UpdateActivity("BAMApiPo", poid.ToString(),
                                     "Received", DateTime.UtcNow,
                                     "Product", xePO.SelectSingleNode("Product").InnerText,
                                     "Amount", xePO.SelectSingleNode("Price").InnerText);
#endif

            // Random Approval Decision
            sleep = r.Next(Global.ApprovalMinTime, Global.ApprovalMaxTime);
            Thread.Sleep(sleep);

            int approve = r.Next(100);
            if (approve > Global.ApprovalPercent)
            {
                Console.WriteLine(xePO.GetAttribute("PoID") + " was Rejected.");

#if Interceptor
                interceptor.OnStep(Global.dataExtractor, "locRejected", xePO, Global.es);
#else
                Global.es.UpdateActivity("BAMApiPo", poid.ToString(),
                                         "Denied", DateTime.UtcNow);
#endif
                return;
            }
            Console.WriteLine(xePO.GetAttribute("PoID") + " was Approved.");

#if Interceptor
            interceptor.OnStep(Global.dataExtractor, "locApproved", xePO, Global.es);
#else
            Global.es.UpdateActivity("BAMApiPo", poid.ToString(),
                                     "Approved", DateTime.UtcNow);
#endif

            // Put the Package in the queue to be shipped
            sleep = r.Next(Global.PackagingMinTime, Global.PackagingMaxTime);
            Thread.Sleep(sleep);
            int packageNumber = r.Next(poid * 10, (poid + 1) * 10);

            if (Global.ShipmentThreads > 0)
            {
                XmlDocument xdShipment = new XmlDocument();
                string      sShipXml   = "<Shipment ShipmentID=\"pkg#" + packageNumber.ToString() + "\"/>";
                xdShipment.LoadXml(sShipXml);
                XmlElement xeShipment    = xdShipment.DocumentElement;
                XmlElement xeShipAddress = xdShipment.CreateElement("Address");
                XmlElement xePoAddress   = (XmlElement)xePO.SelectSingleNode("Address");
                xeShipAddress.InnerText = xePoAddress.InnerText;
                xeShipment.AppendChild(xeShipAddress);

                lock (ShipmentApplication.ShipPackages)
                {
                    ShipmentApplication.ShipPackages.Enqueue(xeShipment);
                }
            }

            // and register this PO to be included in some invoice
            if (Global.InvoiceThreads > 0)
            {
                InvoiceApplication.PosToInvoice.Enqueue(xePO);
            }

            Console.WriteLine(xePO.GetAttribute("PoID") +
                              " was shipped as pkg#" + packageNumber.ToString());

#if Interceptor
            interceptor.OnStep(Global.dataExtractor, "locPackaged", xePO, Global.es);
#else
            Global.es.UpdateActivity("BAMApiPo", poid.ToString(),
                                     "Packaged", DateTime.UtcNow);
            Global.es.EnableContinuation("BAMApiPo", poid.ToString(), "pkg#" + packageNumber.ToString());
            Global.es.EndActivity("BAMApiPo", poid.ToString());
#endif
        }