コード例 #1
0
 static public void Stop()
 {
     if (WorkerThread == null)
     {
         return;
     }
     WorkerThread.Abort();
     WorkerThread.Join();
 }
コード例 #2
0
 public void Abort()
 {
     try {
         if (WorkerThread == null)
         {
             return;
         }
         WorkerThread.Abort();
         WorkerThread.Join(50);
     } catch { }
     WorkerThread = null;
 }
コード例 #3
0
        /// <summary>
        /// Stop this thread.
        /// </summary>
        /// <param name="join">Wait for the thread exit.</param>
        /// <param name="force">Force the thread exit.</param>
        public void Stop(bool join = false, bool force = false)
        {
            Running = false;

            if (force)
            {
                WorkerThread.Abort();
            }

            if (join)
            {
                WorkerThread.Join();
            }
        }
コード例 #4
0
        /// <summary>
        /// Stops the CPU.
        /// </summary>
        public void Stop()
        {
            if (WorkerThread.IsAlive)
            {
                stop = true;

                // TODO: I think this is the right way to do it.
                if (Thread.CurrentThread != WorkerThread)
                {
                    // TODO: I'm not sure if this is a good idea, but it gets the thread to stop.
                    TrapSync.Set();
                    SyscallSync.Set();

                    WorkerThread.Join();
                }
            }

            BreakPoints.Clear();
        }
コード例 #5
0
 void IAsyncDataAdapter.Cancel()
 {
     using (LogFactory.Instance.GetCurrentMethodLog())
     {
         _isCommandCancelled = true;
         if (_thread != null)
         {
             _thread.Stop();
             if (_provider.IsCommandCancelable)
             {
                 ThreadPool.QueueUserWorkItem(CancelWaitCallback);
             }
             else
             {
                 var joined = _thread.Join(5000);
                 if (!joined)
                 {
                     //TODO _thread.Abort();
                 }
             }
         }
     }
 }
コード例 #6
0
        /// <summary>
        /// Terminate any worker threads in this thread group.
        /// Jobs currently in progress will complete.
        /// </summary>
        public virtual void Shutdown(bool waitForJobsToComplete)
        {
            // Give waiting (wait(1000)) worker threads a chance to shut down.
            // Active worker threads will shut down after finishing their
            // current job.
            lock (nextRunnableLock)
            {
                log.Debug("Shutting down threadpool...");

                isShutdown = true;

                if (workers == null) // case where the pool wasn't even initialize()ed
                {
                    return;
                }

                // signal each worker thread to shut down
                foreach (WorkerThread thread in workers)
                {
                    if (thread != null)
                    {
                        thread.Shutdown();
                    }
                }
                Monitor.PulseAll(nextRunnableLock);


                if (waitForJobsToComplete)
                {
                    bool interrupted = false;
                    try
                    {
                        // wait for hand-off in runInThread to complete...
                        while (handoffPending)
                        {
                            try
                            {
                                Monitor.Wait(nextRunnableLock, 100);
                            }
                            catch (ThreadInterruptedException)
                            {
                                interrupted = true;
                            }
                        }

                        // Wait until all worker threads are shut down
                        while (busyWorkers.Count > 0)
                        {
                            LinkedListNode <WorkerThread> wt = busyWorkers.First;
                            try
                            {
                                log.DebugFormat(CultureInfo.InvariantCulture, "Waiting for thread {0} to shut down", wt.Value.Name);

                                // note: with waiting infinite time the
                                // application may appear to 'hang'.
                                Monitor.Wait(nextRunnableLock, 2000);
                            }
                            catch (ThreadInterruptedException)
                            {
                                interrupted = true;
                            }
                        }

                        while (workers.Count > 0)
                        {
                            int          index = workers.Count - 1;
                            WorkerThread wt    = workers[index];
                            try
                            {
                                wt.Join();
                                workers.RemoveAt(index);
                            }
                            catch (ThreadInterruptedException)
                            {
                                interrupted = true;
                            }
                        }
                    }
                    finally
                    {
                        if (interrupted)
                        {
                            Thread.CurrentThread.Interrupt();
                        }
                    }

                    log.Debug("No executing jobs remaining, all threads stopped.");
                }

                log.Debug("Shutdown of threadpool complete.");
            }
        }
コード例 #7
0
        /// <summary>
        /// Terminate any worker threads in this thread group.
        /// Jobs currently in progress will complete.
        /// </summary>
        public virtual void Shutdown(bool waitForJobsToComplete)
        {
            // Give waiting (wait(1000)) worker threads a chance to shut down.
            // Active worker threads will shut down after finishing their
            // current job.
            lock (nextRunnableLock)
            {
                isShutdown = true;

                if (workers == null) // case where the pool wasn't even initialize()ed
                {
                    return;
                }

                // signal each worker thread to shut down
                for (int i = 0; i < workers.Count; i++)
                {
                    if (workers[i] != null)
                    {
                        workers[i].Shutdown();
                    }
                }
                Monitor.PulseAll(nextRunnableLock);


                if (waitForJobsToComplete)
                {
                    // wait for hand-off in runInThread to complete...
                    while (handoffPending)
                    {
                        try
                        {
                            Monitor.Wait(nextRunnableLock, 100);
                        }
                        catch (ThreadInterruptedException)
                        {
                        }
                    }

                    // Wait until all worker threads are shut down
                    while (busyWorkers.Count > 0)
                    {
                        LinkedListNode <WorkerThread> wt = busyWorkers.First;
                        try
                        {
                            // note: with waiting infinite time the
                            // application may appear to 'hang'.
                            Monitor.Wait(nextRunnableLock, 2000);
                        }
                        catch (ThreadInterruptedException)
                        {
                        }
                    }

                    while (workers.Count > 0)
                    {
                        int          index = workers.Count - 1;
                        WorkerThread wt    = workers[index];
                        try
                        {
                            wt.Join();
                            workers.RemoveAt(index);
                        }
                        catch (ThreadStateException)
                        {
                        }
                    }
                }
            }
        }
コード例 #8
0
        public virtual void Shutdown(bool waitForJobsToComplete)
        {
            lock (nextRunnableLock)
            {
                isShutdown = true;

                if (workers == null)
                {
                    return;
                }

                foreach (WorkerThread thread in workers)
                {
                    if (thread != null)
                    {
                        thread.Shutdown();
                    }
                }
                Monitor.PulseAll(nextRunnableLock);

                if (waitForJobsToComplete)
                {
                    bool interrupted = false;
                    try
                    {
                        while (handoffPending)
                        {
                            try
                            {
                                Monitor.Wait(nextRunnableLock, 100);
                            }
                            catch (ThreadInterruptedException)
                            {
                                interrupted = true;
                            }
                        }

                        while (busyWorkers.Count > 0)
                        {
                            LinkedListNode <WorkerThread> wt = busyWorkers.First;
                            try
                            {
                                log.DebugFormat(CultureInfo.InvariantCulture, "Waiting for thread {0} to shut down", wt.Value.Name);

                                Monitor.Wait(nextRunnableLock, 2000);
                            }
                            catch (ThreadInterruptedException)
                            {
                                interrupted = true;
                            }
                        }

                        while (workers.Count > 0)
                        {
                            int          index = workers.Count - 1;
                            WorkerThread wt    = workers[index];
                            try
                            {
                                wt.Join();
                                workers.RemoveAt(index);
                            }
                            catch (ThreadInterruptedException)
                            {
                                interrupted = true;
                            }
                        }
                    }
                    finally
                    {
                        if (interrupted)
                        {
                            Thread.CurrentThread.Interrupt();
                        }
                    }

                    log.Debug("No executing jobs remaining, all threads stopped.");
                }

                log.Debug("Shutdown of threadpool complete.");
            }
        }
コード例 #9
0
        static bool DownloadDependencies(string RootPath, IEnumerable <DependencyFile> RequiredFiles, IEnumerable <DependencyBlob> Blobs, IEnumerable <DependencyPack> Packs, int NumThreads, int MaxRetries, string ProxyUrl)
        {
            // Make sure we can actually open the right number of connections
            ServicePointManager.DefaultConnectionLimit = NumThreads;

            // Build a lookup for the files that need updating from each blob
            Dictionary <string, List <DependencyFile> > BlobToFiles = new Dictionary <string, List <DependencyFile> >();

            foreach (DependencyFile RequiredFile in RequiredFiles)
            {
                List <DependencyFile> FileList;
                if (!BlobToFiles.TryGetValue(RequiredFile.Hash, out FileList))
                {
                    FileList = new List <DependencyFile>();
                    BlobToFiles.Add(RequiredFile.Hash, FileList);
                }
                FileList.Add(RequiredFile);
            }

            // Find all the required blobs
            DependencyBlob[] RequiredBlobs = Blobs.Where(x => BlobToFiles.ContainsKey(x.Hash)).ToArray();

            // Build a lookup for the files that need updating from each blob
            Dictionary <string, List <DependencyBlob> > PackToBlobs = new Dictionary <string, List <DependencyBlob> >();

            foreach (DependencyBlob RequiredBlob in RequiredBlobs)
            {
                List <DependencyBlob> BlobList = new List <DependencyBlob>();
                if (!PackToBlobs.TryGetValue(RequiredBlob.PackHash, out BlobList))
                {
                    BlobList = new List <DependencyBlob>();
                    PackToBlobs.Add(RequiredBlob.PackHash, BlobList);
                }
                BlobList.Add(RequiredBlob);
            }

            // Find all the required packs
            DependencyPack[] RequiredPacks = Packs.Where(x => PackToBlobs.ContainsKey(x.Hash)).ToArray();

            // Get temporary filenames for all the files we're going to download
            Dictionary <DependencyPack, string> DownloadFileNames = new Dictionary <DependencyPack, string>();

            foreach (DependencyPack Pack in RequiredPacks)
            {
                DownloadFileNames.Add(Pack, Path.GetTempFileName());
            }

            // Setup the async state
            AsyncDownloadState State = new AsyncDownloadState();

            State.NumFiles = RequiredFiles.Count();
            long NumBytesTotal = RequiredPacks.Sum(x => x.CompressedSize);
            ConcurrentQueue <DependencyPack> DownloadQueue   = new ConcurrentQueue <DependencyPack>(RequiredPacks);
            ConcurrentQueue <DependencyPack> DecompressQueue = new ConcurrentQueue <DependencyPack>();

            // Create all the worker threads
            Thread[] WorkerThreads = new Thread[NumThreads];
            for (int Idx = 0; Idx < NumThreads; Idx++)
            {
                WorkerThreads[Idx] = new Thread(x => DownloadWorker(RootPath, DownloadQueue, DecompressQueue, DownloadFileNames, PackToBlobs, BlobToFiles, State, MaxRetries, ProxyUrl));
                WorkerThreads[Idx].Start();
            }

            // Create the decompression thread
            Thread DecompressionThread = new Thread(x => DecompressWorker(RootPath, DecompressQueue, DownloadFileNames, PackToBlobs, BlobToFiles, State));

            DecompressionThread.Start();

            // Tick the status message until we've finished or ended with an error. Use a circular buffer to average out the speed over time.
            long[] NumBytesReadBuffer = new long[60];
            for (int BufferIdx = 0, NumFilesReportedRead = 0; NumFilesReportedRead < State.NumFiles && State.NumFailingDownloads < NumThreads && State.LastDecompressError == null; BufferIdx = (BufferIdx + 1) % NumBytesReadBuffer.Length)
            {
                const int TickInterval = 100;

                long  NumBytesRead      = Interlocked.Read(ref State.NumBytesRead);
                float NumBytesPerSecond = (float)Math.Max(NumBytesRead - NumBytesReadBuffer[BufferIdx], 0) * 1000.0f / (NumBytesReadBuffer.Length * TickInterval);
                NumFilesReportedRead = State.NumFilesRead;
                Log.WriteStatus("Received {0}/{1} files ({2:0.0}/{3:0.0}mb; {4:0.00}mb/s; {5}%)...", NumFilesReportedRead, State.NumFiles, (NumBytesRead / (1024.0 * 1024.0)) + 0.0999999, (NumBytesTotal / (1024.0 * 1024.0)) + 0.0999999, (NumBytesPerSecond / (1024.0 * 1024.0)) + 0.0099, (NumBytesRead * 100) / NumBytesTotal);
                NumBytesReadBuffer[BufferIdx] = NumBytesRead;

                Thread.Sleep(TickInterval);
            }

            // If we finished with an error, try to clean up and return
            if (State.NumFilesRead < State.NumFiles)
            {
                DecompressionThread.Abort();
                foreach (Thread WorkerThread in WorkerThreads)
                {
                    WorkerThread.Abort();
                }
                Log.WriteError("{0}", (State.LastDecompressError != null)? State.LastDecompressError : State.LastDownloadError);
                foreach (string FileName in DownloadFileNames.Values)
                {
                    try { File.Delete(FileName); } catch (Exception) { }
                }
                return(false);
            }

            // Join all the threads
            DecompressionThread.Join();
            foreach (Thread WorkerThread in WorkerThreads)
            {
                WorkerThread.Join();
            }
            Log.FlushStatus();
            return(true);
        }