BCNet.Bitcoin.BCUpdateThread C# (CSharp) Method

BCUpdateThread() private method

private BCUpdateThread ( ) : void
return void
        void BCUpdateThread()
        {
            LoadBlocks();

            while (true)
            {
                if (mDeadNodes.Count > 0)
                {
                    mNodeLock.WaitOne();
                    foreach (NodeConnection n in mDeadNodes)
                    {
                        n.Destroy();
                    }
                    mDeadNodes.Clear();
                    mNodeLock.ReleaseMutex();
                }

                // Merge pending headers
                if (mPendingHeaders != null)
                {
                    MergePendingHeaders();
                    mPendingHeaders = null;
                    mGettingHeaders = false;
                }

                // Check for larger block chains
                if (!mGettingHeaders)
                {
                    mNodeLock.WaitOne();
                    foreach (NodeConnection n in mNodes)
                    {
                        if (n.mRemoteHeight > mCurrentHeight)
                        {
                            byte[] originHash = null;
                            if (mBlocks.Count > 0)
                            {
                                mBlockLock.WaitOne();
                                originHash = mBlocks[mBlocks.Count - 1].mHeader.mHash;
                                mBlockLock.ReleaseMutex();
                            }
                            n.RequestHeaders(originHash);
                            Console.WriteLine("Requesting Headers " + mCurrentHeight + " / " + n.mRemoteHeight);
                            mGettingHeaders = true;
                            mLastHeaderRequest = DateTime.Now;
                            break;
                        }
                    }
                    mNodeLock.ReleaseMutex();
                }
                else
                {
                    TimeSpan s = DateTime.Now - mLastHeaderRequest;
                    if (s.TotalSeconds > 15)
                    {
                        // Abandon this request and move this connection to the bottom
                        mNodeLock.WaitOne();
                        NodeConnection n = mNodes[0];
                        mNodes.RemoveAt(0);
                        mNodes.Add(n);
                        mNodeLock.ReleaseMutex();
                        n.FetchingHeaders = false;
                        mGettingHeaders = false;
                    }
                }

                RequestBlocks();

                ArchiveBlocks();

                Thread.Sleep(100);
            }
        }