Exemple #1
0
        private async void SendDataAsync(string name)
        {
            var rand = new Random(OperationContext.Current.GetHashCode() - Environment.TickCount);

            await Task.Factory.StartNew(() =>
            {
                var basePrice = 100.0 + ((rand.NextDouble() - 0.5) * 10.0);

                while (subscribers.ContainsKey(name))
                {
                    var loopTime = Environment.TickCount;

                    //Calc a new price
                    basePrice += (rand.NextDouble() - 0.5) * 0.1;
                    var price  = basePrice + Math.Sin(loopTime * 0.001);

                    var data             = new BondData(name, price);
                    ICallback subscriber = null;
                    try
                    {
                        //Broadcast to subscribers
                        foreach (var callback in subscribers[name])
                        {
                            subscriber = callback;
                            callback.NewData(data);
                        }
                    }
                    catch (TimeoutException)
                    {
                        Console.WriteLine("{0} subscriber has timed out and will be removed", name);

                        RemoveSubscriber(name, subscriber);
                    }
                    catch (InvalidOperationException)
                    {
                        Console.WriteLine("SendDataAsync({0}): Subscriber list changed while processing", name);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(name);
                        Console.WriteLine(e.GetType());
                        Console.WriteLine(e.Message);
                    }

                    Sync(loopTime);
                }
            },
                                        TaskCreationOptions.LongRunning);

            Console.WriteLine("SendDataAsync({0}): No more subscribers, price generation has stopped", name);
        }
Exemple #2
0
        private async void SendDataAsync(string name)
        {
            var rand = new Random(OperationContext.Current.GetHashCode() - Environment.TickCount);

            await Task.Factory.StartNew(() => 
            {
                var basePrice = 100.0 + ((rand.NextDouble() - 0.5) * 10.0);

                while (subscribers.ContainsKey(name))
                {
                    var loopTime = Environment.TickCount;

                    //Calc a new price
                    basePrice += (rand.NextDouble() - 0.5) * 0.1;
                    var price = basePrice + Math.Sin(loopTime * 0.001);

                    var data = new BondData(name, price);
                    ICallback subscriber = null;
                    try
                    {
                        //Broadcast to subscribers
                        foreach (var callback in subscribers[name])
                        {
                            subscriber = callback;
                            callback.NewData(data);
                        }
                    }
                    catch (TimeoutException)
                    {
                        Console.WriteLine("{0} subscriber has timed out and will be removed", name);

                        RemoveSubscriber(name, subscriber);
                    }
                    catch (InvalidOperationException)
                    {
                        Console.WriteLine("SendDataAsync({0}): Subscriber list changed while processing", name);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(name);
                        Console.WriteLine(e.GetType());
                        Console.WriteLine(e.Message);
                    }

                    Sync(loopTime);
                }
            },
            TaskCreationOptions.LongRunning);

            Console.WriteLine("SendDataAsync({0}): No more subscribers, price generation has stopped", name);
        }