Exemplo n.º 1
0
 static void WatchdogThread()
 {
     for (; ;)
     {
         Thread.Sleep(2000);
         lock (AvailableConnections)
         {
             if (Capacity > 1)
             {
                 try
                 {
                     if (AvailableConnections.Count > WaitingThreads)
                     {
                         Monitor.Wait(AvailableConnections);
                         BasicConnection conn = AvailableConnections.Dequeue();
                         Connections.Remove(conn);
                         Count--;
                     }
                 }
                 catch (Exception e)
                 {
                     MessageBox.Show(e.ToString());
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
 void WorkerThread()
 {
     while (!Cancelled)
     {
         BasicConnection conn = new BasicConnection();
         conn.DoWork("Hello");
         conn.DoWork("Goodbye");
     }
 }
Exemplo n.º 3
0
        static void CreateNewPooledConnection()
        {
            Debug.WriteLine("Creating new Basic connection...");
            BasicConnection conn = new BasicConnection();

            lock (AvailableConnections)
            {
                Connections.Add(conn);
            }

            PerfmonCounters.BasicConnections.Increment();
            PerfmonCounters.CurrentPoolSize.RawValue = Count;
            DisposeConnection(conn);
        }
Exemplo n.º 4
0
        public static void DisposeConnection(BasicConnection conn)
        {
            lock (AvailableConnections)
            {
                PerfmonCounters.CurrentPoolSize.RawValue = Count;

                //if the capacity has shrunk then we don't
                //put this in the pool.
                if (Count <= Capacity)
                {
                    AvailableConnections.Enqueue(conn);
                    Monitor.Pulse(AvailableConnections);
                }
                else
                {
                    Count--;
                    Debug.WriteLine("Discarding connection");
                    Connections.Remove(conn);
                }
            }
        }
Exemplo n.º 5
0
        public static BasicConnection GetConnection()
        {
            BasicConnection retval = null;

            lock (AvailableConnections)
            {
                PerfmonCounters.MaxPoolCapacity.RawValue = Capacity;
                PerfmonCounters.CurrentPoolSize.RawValue = Count;

                if (AvailableConnections.Count > 0 && WaitingThreads == 0)
                {
                    //if we have available connections and no one is waiting
                    //then we just return immediately.
                    retval = AvailableConnections.Dequeue();
                }
                else
                {
                    if (AvailableConnections.Count == 0 && Count < Capacity)
                    {
                        Count++;
                        Thread t = new Thread(CreateNewPooledConnection);
                        t.Start();
                    }

                    //If there are no connections available (or there are but
                    //other threads already waiting) then we have to get
                    //in line and wait for one to be released.
                    WaitingThreads++;
                    Monitor.Wait(AvailableConnections);
                    WaitingThreads--;

                    retval = AvailableConnections.Dequeue();
                }
            }

            return(retval);
        }