Example #1
0
 /// <summary>
 /// Queue thread for processing
 /// </summary>
 /// <remarks>
 /// Typically only used by queued threads to add work items to the queue
 /// </remarks>
 internal static void Queue(ManagedThread item)
 {
     lock (m_queuedThreads)
     {
         m_queuedThreads.AddLast(item);
     }
 }
Example #2
0
 /// <summary>
 /// Remove completed thread from active thread list
 /// </summary>
 internal static void Remove(ManagedThread item)
 {
     lock (m_queuedThreads)
     {
         m_activeThreads.Remove(item);
     }
 }
Example #3
0
 /// <summary>
 /// Add an item to the active thread list
 /// </summary>
 /// <remarks>
 /// Typically only used by standard threads when user calls "Start"
 /// </remarks>
 internal static void Add(ManagedThread item)
 {
     // Standard threads are simply added to the active thread list when started
     lock (m_queuedThreads)
     {
         item.Status = ThreadStatus.Started;
         m_activeThreads.AddLast(item);
     }
 }
Example #4
0
        /// <summary>
        /// Queues a work item for processing on the managed thread pool
        /// </summary>
        /// <param name="callback">A WaitCallback representing the method to execute.</param>
        /// <returns>Reference to queued thread</returns>
        /// <remarks>
        /// This differs from the normal thread pool QueueUserWorkItem function in that it does
        /// not return a success value determing if item was queued, but rather a reference to
        /// to the managed thread that was actually placed on the queue.
        /// </remarks>
        public static ManagedThread QueueUserWorkItem(ThreadStart callback)
        {
            if (callback == null) throw (new ArgumentNullException("callback"));

            ManagedThread item = new ManagedThread(ThreadType.QueuedThread, callback, null, null);

            ManagedThreads.Queue(item);

            ThreadPool.QueueUserWorkItem(HandleItem);

            return item;
        }
Example #5
0
        /// <summary>
        /// Queues a work item for processing on the managed thread pool
        /// </summary>
        /// <param name="callback">A WaitCallback representing the method to execute.</param>
        /// <param name="state">An object containing data to be used by the method.</param>
        /// <param name="ctx">Alternate execution context in which to run the thread.</param>
        /// <returns>Reference to queued thread</returns>
        /// <remarks>
        /// This differs from the normal thread pool QueueUserWorkItem function in that it does
        /// not return a success value determing if item was queued, but rather a reference to
        /// to the managed thread that was actually placed on the queue.
        /// </remarks>
        public static ManagedThread QueueUserWorkItem(ContextCallback callback, object state, ExecutionContext ctx)
        {
            if (callback == null) throw (new ArgumentNullException("callback"));

            ManagedThread item = new ManagedThread(ThreadType.QueuedThread, callback, state, ctx);

            ManagedThreads.Queue(item);

            ThreadPool.QueueUserWorkItem(HandleItem);

            return item;
        }
Example #6
0
        /// <summary>
        /// Removes a queued thread from thread pool if still queued, if allowAbort is True
        /// aborts the thread if executing (standard or queued)
        /// </summary>
        /// <param name="item">Thread to cancel</param>
        /// <param name="allowAbort">Set to True to abort thread if executing</param>
        /// <param name="stateInfo">An object that contains application-specific information, such as state, which can be used by the thread being aborted.</param>
        public static void Cancel(ManagedThread item, bool allowAbort, object stateInfo)
        {
            if (item == null) throw new ArgumentNullException("item");

            LinkedListNode<ManagedThread> node;

            lock (m_queuedThreads)
            {
                // Change thread status to aborted
                item.Status = ThreadStatus.Aborted;

                // See if item is still queued for execution in thread pool
                node = m_queuedThreads.Find(item);

                // Handle abort or dequeue
                if (node == null)
                {
                    if (allowAbort)
                    {
                        // Started items may be aborted, even if running in thread pool
                        if (stateInfo == null)
                            item.Thread.Abort();
                        else
                            item.Thread.Abort(stateInfo);
                    }
                }
                else
                {
                    // Remove item from queue if queued thread has yet to start
                    m_queuedThreads.Remove(node);
                }
            }
        }
Example #7
0
        private static string ThreadStatusText(ManagedThread item)
        {
            string runtime = Seconds.ToText(item.RunTime);

            switch (item.Status)
            {
                case ThreadStatus.Unstarted:
                    return "Not Started";
                case ThreadStatus.Queued:
                    return "Queued";
                case ThreadStatus.Executing:
                    return "Executing for " + runtime;
                case ThreadStatus.Completed:
                    return "Completed in " + runtime;
                case ThreadStatus.Aborted:
                    return "Aborted, ran for " + runtime;
                default:
                    return "Status Unknown";
            }
        }
Example #8
0
        /// <summary>
        /// Queues a work item for processing on the managed thread pool
        /// </summary>
        /// <param name="callback">A WaitCallback representing the method to execute.</param>
        /// <param name="state">An object containing data to be used by the method.</param>
        /// <returns>Reference to queued thread</returns>
        /// <remarks>
        /// This differs from the normal thread pool QueueUserWorkItem function in that it does
        /// not return a success value determing if item was queued, but rather a reference to
        /// to the managed thread that was actually placed on the queue.
        /// </remarks>
        public static ManagedThread QueueUserWorkItem(ParameterizedThreadStart callback, object state)
        {
            if ((object)callback == null)
                throw (new ArgumentNullException("callback"));

            ManagedThread item = new ManagedThread(ThreadType.QueuedThread, callback, state, null);

            ManagedThreads.Queue(item);

            ThreadPool.QueueUserWorkItem(HandleItem);

            return item;
        }
Example #9
0
        internal void StartAsyncTransfer()
        {
#if ThreadTracking
            ManagedThread thread = new ManagedThread(TransferThreadProc);
            thread.Name = "TVA.Net.Ftp.FileTransferer.TransferThreadProc() [" + m_remoteFile + "]";
#else
            Thread thread = new Thread(TransferThreadProc);
            thread.Name = "Transfer file thread: " + m_remoteFile;
#endif
            thread.Start();
        }
Example #10
0
        public override void Start()
        {
            if (Enabled && !IsRunning && ValidConfigurationString(ConfigurationString))
            {
                try
                {
                    int serverPort = 0;

                    if (m_configurationData.ContainsKey("port"))
                        serverPort = int.Parse(m_configurationData["port"]);

                    m_udpServer = new StateInfo<Socket>();
                    m_udpServer.ID = ServerID;
                    m_udpServer.Passphrase = HandshakePassphrase;
                    m_udpServer.Client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                    if (Handshake)
                    {
                        // We will listen for data only when Handshake is enabled and a valid port has been specified in
                        // the configuration string. We do this in order to keep the server stable and besides that, the
                        // main purpose of a UDP server is to serve data in most cases.
                        if (serverPort > 0)
                        {
                            m_udpServer.Client.Bind(new IPEndPoint(IPAddress.Any, serverPort));

#if ThreadTracking
                            ManagedThread receiveThread = new ManagedThread(ReceiveClientData);
                            receiveThread.Name = "TVA.Communication.UdpServer.ReceiveClientData() [" + ServerID.ToString() + "]";
#else
							Thread receiveThread = new Thread(ReceiveClientData);
#endif
                            receiveThread.Start();
                        }
                        else
                        {
                            throw new ArgumentException("Server port must be specified in the configuration string.");
                        }
                    }

                    OnServerStarted(EventArgs.Empty);

                    if (!Handshake && m_configurationData.ContainsKey("clients"))
                    {
                        // We will ignore the client list in configuration string when Handshake is enabled.
                        foreach (string clientString in m_configurationData["clients"].Replace(" ", "").Split(','))
                        {
                            try
                            {
                                int clientPort = 0;

                                string[] clientStringSegments = clientString.Split(':');

                                if (clientStringSegments.Length == 2)
                                    clientPort = int.Parse(clientStringSegments[1]);

                                StateInfo<IPEndPoint> udpClient = new StateInfo<IPEndPoint>();
                                udpClient.ID = Guid.NewGuid();
                                udpClient.Client = Transport.GetIpEndPoint(clientStringSegments[0], clientPort);

                                lock (m_udpClients)
                                {
                                    m_udpClients.Add(udpClient.ID, udpClient);
                                }

                                OnClientConnected(udpClient.ID);
                            }
                            catch
                            {
                                // Ignore invalid client entries.
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    OnServerStartupException(ex);
                }
            }
        }