Ejemplo n.º 1
0
        /// <summary>
        /// Common method used to check if the Network Messages are current, if not they will be updated and a StatusUpdate
        /// will occur.
        /// <remarks>
        /// This method contains Locks and will block, so make sure you call it from a Thread and not the Main UI Thread!
        /// </remarks>
        /// </summary>
        public static void CheckNetworkMessages(FileData file, Action <IStatusModel> statusUpdate, string source)
        {
            var status       = $"Building {source} Network Messages";
            var statusDetail = $"{file.SourceType} - {file.Count} lines";

            if (statusUpdate != null)
            {
                lock (CheckingLockObject)
                {
                    if (Interlocked.Read(ref _checkingCount) > 0)
                    {
                        statusUpdate(StatusModel.Update(status, statusDetail + Environment.NewLine + "Another process is building the network messages..."));
                    }

                    if (file.IsCached_Lazy_NetworkMessages)
                    {
                        return;
                    }
                    Interlocked.Increment(ref _checkingCount);
                }
            }

            try
            {
                // Only allow 1 Thread to build the same file at a time.
                lock (BuildingNetworkMessagesLockObject)
                {
                    try
                    {
                        if (statusUpdate != null)
                        {
                            statusUpdate(StatusModel.StartBackground);
                            statusUpdate(StatusModel.StartStopWatch);
                            statusUpdate(StatusModel.Update(status, statusDetail));
                        }
                        FileService.BuildNetworkMessages(file);
                    }
                    finally
                    {
                        if (statusUpdate != null)
                        {
                            statusUpdate(StatusModel.EndBackground);
                            statusUpdate(StatusModel.Completed);
                        }
                    }
                }
            }
            finally
            {
                Interlocked.Decrement(ref _checkingCount);
            }
        }