Пример #1
0
 /// <summary>
 /// Set up Queuing mechanisms.
 /// </summary>
 /// <param name="port">Port for client connection.</param>
 /// <param name="threadProcessor">Mechanism for handling responses.</param>
 public ThreadlessUdpNetworkClient(int port, IThreadProcessor threadProcessor)
     : base(port)
 {
     _threadProcessor = threadProcessor;
 }
Пример #2
0
        protected virtual void RunThread(object arg)
        {
            int threadIndex = (int)arg;
            IThreadProcessor <In, Out> processor = processors[threadIndex];
            Stopwatch watch = new Stopwatch();

            const int IDLE_TIME = 5;

            while (IsRunning)
            {
                if (pending.Count > 0)
                {
                    ThreadedRequest <In, Out> todo = null;
                    lock (KEY)
                    {
                        if (pending.Count > 0)
                        {
                            todo = pending.Dequeue();
                        }
                    }

                    if (todo != null && !todo.IsCancelled)
                    {
                        Statistics[threadIndex].CumulativeProcessed++;
                        _processedIncrement++;
                        watch.Start();
                        Out output = default(Out);
                        try
                        {
                            output = processor.Process(todo.InputArgs);
                        }
                        catch (Exception)
                        {
                            PostResult(() =>
                            {
                                todo.UponProcessed?.Invoke(ThreadedRequestResult.Error, output);
                                todo.ReturnToPool();
                            });
                            continue;
                        }
                        PostResult(() =>
                        {
                            todo.UponProcessed?.Invoke(ThreadedRequestResult.Run, output);
                            todo.ReturnToPool();
                        });
                        watch.Stop();
                        Statistics[threadIndex].CumulativeUsage += (float)watch.Elapsed.TotalSeconds;
                        if (Statistics[threadIndex].CumulativeUsage > 1f)
                        {
                            Statistics[threadIndex].CumulativeUsage = 1f;
                        }
                        Statistics[threadIndex].AddProcessTime((float)watch.Elapsed.TotalSeconds);
                        watch.Reset();
                    }
                    else if (todo != null && todo.IsCancelled)
                    {
                        PostResult(() =>
                        {
                            todo.UponProcessed?.Invoke(ThreadedRequestResult.Cancelled, default(Out));
                            todo.ReturnToPool();
                        });
                    }
                }
                else
                {
                    Thread.Sleep(IDLE_TIME);
                }
            }
        }
 /**
  * constructor
  *
  * @processor: a particular processor you like to use
  * */
 public Example1Processor(IThreadProcessor <G> processor)
 {
     m_ThreadProcessor = processor;
 }