Пример #1
0
 /// <summary>
 /// filewriting thread's loop
 /// </summary>
 void threadproc()
 {
     try
     {
         while (true)
         {
             Object o = threadQ.Take();
             if (o is IAsyncResult)
             {
                 jmdfile.AddVideo(GzipFrameDelegate.EndInvoke((IAsyncResult)o));
             }
             else if (o is short[])
             {
                 jmdfile.AddSamples((short[])o);
             }
             else
             {
                 // anything else is assumed to be quit time
                 return;
             }
         }
     }
     catch (Exception e)
     {
         System.Windows.Forms.MessageBox.Show("JMD Worker Thread died:\n\n" + e);
         return;
     }
 }
    static void Main(string[] args)
    {
        var c = new System.Collections.Concurrent.BlockingCollection <Tuple <bool, Action> >();
        var t = new Thread(() =>
        {
            while (true)
            {
                var item = c.Take();
                if (!item.Item1)
                {
                    break;
                }
                item.Item2();
            }
            Console.WriteLine("Exiting thread");
        });

        t.Start();

        Console.WriteLine("Press any key to queue first action");
        Console.ReadKey();
        c.Add(Tuple.Create <bool, Action>(true, () => Console.WriteLine("Executing first action")));

        Console.WriteLine("Press any key to queue second action");
        Console.ReadKey();
        c.Add(Tuple.Create <bool, Action>(true, () => Console.WriteLine("Executing second action")));
        Console.WriteLine("Press any key to stop the thread");
        Console.ReadKey();
        c.Add(Tuple.Create <bool, Action>(false, null));

        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
    }
Пример #3
0
 void threadproc()
 {
     try
     {
         while (true)
         {
             Object o = threadQ.Take();
             if (o is IVideoProvider)
             {
                 AddFrameEx((IVideoProvider)o);
             }
             else if (o is short[])
             {
                 AddSamplesEx((short[])o);
             }
             else
             {
                 // anything else is assumed to be quit time
                 return;
             }
         }
     }
     catch (Exception e)
     {
         System.Windows.Forms.MessageBox.Show("AVIFIL32 Thread died:\n\n" + e);
         return;
     }
 }
Пример #4
0
 private void threadproc()
 {
     try
     {
         while (true)
         {
             object o = threadQ.Take();
             if (o is IVideoProvider)
             {
                 AddFrameEx((IVideoProvider)o);
             }
             else if (o is short[])
             {
                 AddSamplesEx((short[])o);
             }
             else
             {
                 // anything else is assumed to be quit time
                 return;
             }
         }
     }
     catch (Exception e)
     {
         MessageBox.Show($"AVIFIL32 Thread died:\n\n{e}");
     }
 }
Пример #5
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            string s = _texts.Take();

            byte[] encodedText = Encoding.UTF8.GetBytes(s);
            encodedText.CopyTo(buffer, offset);
            return(encodedText.Length);
        }
Пример #6
0
        public string Call(string message)
        {
            var messageBytes = Encoding.UTF8.GetBytes(message);

            channel.BasicPublish(exchange: "", routingKey: "rpc_queue", basicProperties: props, body: messageBytes);

            channel.BasicConsume(consumer: consumer, queue: replyQueueName, noAck: false);

            return(respQueue.Take());
        }
Пример #7
0
        private void BackgroundReinitialise()
        {
            T item = default(T);

            while (!_ItemsToInitialise.IsCompleted)
            {
                try
                {
                    item = _ItemsToInitialise.Take();
                }
                catch (InvalidOperationException)
                {
                    if (_ItemsToInitialise.IsCompleted)
                    {
                        return;
                    }
                }

                if (item != null)
                {
                    if (IsDisposed)
                    {
                        SafeDispose(item);
                    }
                    else
                    {
                        if (PoolPolicy.ReinitializeObject != null)
                        {
                            PoolPolicy.ReinitializeObject(item);
                        }

                        if (ShouldReturnToPool(item))
                        {
                            _Pool.Add(item);

                            Interlocked.Increment(ref _PoolInstancesCount);
                        }
                    }
                }
            }
        }
Пример #8
0
        /// <summary>
        ///
        /// </summary>
        private void QueueToWrite()
        {
            while (true)
            {
                StrongBox <QueueMessage> item = _queue.Take();
                Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
                QueueMessage qm = item.Value;
                item.Value = null;

                if (!BasicEnqueue(qm.exchangeName, qm.queueName, qm.data, qm.persistent, qm.durableQueue, qm.exchangeType))
                {
                    loger.LogError(new Exception(), qm);
                }

                qm   = null;
                item = null;
            }
        }
Пример #9
0
 public override int Read(byte[] buffer, int offset, int count)
 {
     try
     {
         var    s           = _texts.Take(_cancellationTokenSource.Token);
         byte[] encodedText = Encoding.UTF8.GetBytes(s);
         encodedText.CopyTo(buffer, offset);
         return(encodedText.Length);
     }
     catch (OperationCanceledException)
     {
         if (_throw != null)
         {
             var ex = _throw;
             _throw = null;
             throw ex;
         }
         return(0);
     }
 }
Пример #10
0
 static LogUtil()
 {
     logThread = new Thread(() => {
         using (StreamWriter sw = new StreamWriter(Constant.LOG_FILE_PATH, true))
         {
             while (state)
             {
                 try
                 {
                     String msg = messageQueue.Take();
                     sw.WriteLine(msg);
                     sw.Flush();
                 }
                 catch (Exception e)
                 {
                 }
             }
         }
     });
     logThread.IsBackground = true;
     logThread.Start();
 }
Пример #11
0
 public double[][] receive()
 {
     return(messageQueue.Take());
 }
        /// <summary>
        ///
        /// </summary>
        private void QueueToWrite()
        {
            //foreach (StrongBox<QueueMessage> item in _queue.GetConsumingEnumerable())
            //{
            //    QueueMessage qm = item.Value;
            //    if (!BasicEnqueue(qm.exchangeName, qm.queueName, qm.data, qm.persistent, qm.durableQueue, qm.exchangeType))
            //    {
            //        loger.LogError(new Exception(), qm);
            //    }

            //    qm = null;
            //    item.Value = null;
            //    //GC.Collect();
            //}

            while (true)
            {
                StrongBox <QueueMessage> item = _queue.Take();
                //Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
                QueueMessage qm = item.Value;
                item.Value = null;

                if (!BasicEnqueue(qm.exchangeName, qm.queueName, qm.data, qm.persistent, qm.durableQueue, qm.exchangeType))
                {
                    loger.LogError(new Exception(), qm);
                }

                qm   = null;
                item = null;
            }

            //while (true)
            //{
            //    resetEvent.Reset();
            //    if (_queue.Count > 0)
            //    {
            //        isQueueToWrite = true;
            //        QueueMessage qm = null;
            //        do
            //        {
            //            lock (_queue)
            //            {
            //                try
            //                {
            //                    qm = (QueueMessage)_queue.Dequeue();
            //                }
            //                catch { }
            //            }
            //            if (qm != null)
            //            {
            //                if (!BasicEnqueue(qm.exchangeName, qm.queueName, qm.data, qm.persistent, qm.durableQueue, qm.exchangeType))
            //                {
            //                    lock (_queue)
            //                    {
            //                        _queue.Enqueue(qm);
            //                    }
            //                }
            //                else
            //                {
            //                    break;
            //                }
            //            }

            //            //SpinWait.SpinUntil(() => false, 1);
            //        } while (qm != null);

            //        //while (_queue.Dequeue(out qm))
            //        //{
            //        //    if (!BasicEnqueue(qm.exchangeName, qm.queueName, qm.data, qm.persistent, qm.durableQueue, qm.exchangeType))
            //        //    {
            //        //        _queue.Enqueue(qm);
            //        //    }
            //        //    else
            //        //    {
            //        //        break;
            //        //    }
            //        //    SpinWait.SpinUntil(() => false, 1);
            //        //}
            //        qm = null;
            //        isQueueToWrite = false;
            //    }

            //    resetEvent.WaitOne(waitMillisecondsTimeout);
            //    //SpinWait.SpinUntil(() => false, waitMillisecondsTimeout);
            //    //Thread.Sleep(waitMillisecondsTimeout);
            //}
        }