Esempio n. 1
0
        internal object GetValue(Guid WatchedGuid, string type)
        {
            //Jams the current thread until the value is returned or the KillReturnWatch flag is set to true

            using (new TimePeriod(1))
            {
                ConsoleEx.WriteLine("GetValue:Awaiting -> " + type.ToString());
                //spec.OnSyncedMessageStart(null);
                spec.Connector.hub.QueueMessage(new NetCoreAdvancedMessage("{EVENT_SYNCEDMESSAGESTART}"));

                attemptsAtReading = 0;

                //If we're this deep, something went really wrong so we just emergency abort
                if (StackFrameHelper.GetCallStackDepth() > 2000)
                {
                    KillReturnWatch = true;
                    throw new CustomException("A fatal error has occurred. Please send this to the devs. You should save your Stockpile then restart the RTC.", Environment.StackTrace);
                }

                while (!SyncReturns.ContainsKey(WatchedGuid))
                {
                    if (KillReturnWatch)
                    {
                        //Stops waiting and returns null
                        KillReturnWatch   = false;
                        attemptsAtReading = 0;

                        ConsoleEx.WriteLine("GetValue:Killed -> " + type.ToString());
                        //spec.OnSyncedMessageEnd(null);
                        spec.Connector.hub.QueueMessage(new NetCoreAdvancedMessage("{EVENT_SYNCEDMESSAGEEND}"));
                        return(null);
                    }

                    attemptsAtReading++;
                    if (attemptsAtReading % 5 == 0)
                    {
                        System.Windows.Forms.Application.DoEvents();                         //This is a horrible hack we need due to the fact we have synchronous calls that invoke the main thread
                    }

                    Thread.Sleep(spec.messageReadTimerDelay);
                }

                attemptsAtReading = 0;
                SyncReturns.TryRemove(WatchedGuid, out object ret);

                ConsoleEx.WriteLine("GetValue:Returned -> " + type.ToString());
                //spec.OnSyncedMessageEnd(null);
                spec.Connector.hub.QueueMessage(new NetCoreAdvancedMessage("{EVENT_SYNCEDMESSAGEEND}"));
                return(ret);
            }
        }
Esempio n. 2
0
        internal async Task <Object> GetValueTask(Guid WatchedGuid, string type, CancellationToken token)
        {
            token.ThrowIfCancellationRequested();
            logger.Trace("GetValue called on {guid}", guid);
            //Jams the current thread until the value is returned or the KillReturnWatch flag is set to true

            logger.Trace("GetValue:Awaiting -> " + type);
            //spec.OnSyncedMessageStart(null);
            spec.Connector.hub.QueueMessage(new NetCoreAdvancedMessage("{EVENT_SYNCEDMESSAGESTART}"));

            var attemptsAtReading = 0;

            //If we're this deep, something went really wrong so we just emergency abort
            if (StackFrameHelper.GetCallStackDepth() > 2000)
            {
                cts.Cancel();
                throw new CustomException("A fatal error has occurred. Please send this to the devs. You should save your Stockpile then restart the RTC.", Environment.StackTrace);
            }

            while (!SyncReturns.ContainsKey(WatchedGuid))
            {
                if (token.IsCancellationRequested)
                {
                    logger.Warn("GetValue:Killed -> " + type);
                    //spec.OnSyncedMessageEnd(null);
                    spec.Connector.hub.QueueMessage(new NetCoreAdvancedMessage("{EVENT_SYNCEDMESSAGEEND}"));
                    throw new OperationCanceledException();
                }

                attemptsAtReading++;
                if (attemptsAtReading % 5 == 0)
                {
                    System.Windows.Forms.Application.DoEvents(); //This is a horrible hack we need due to the fact we have synchronous calls that invoke the main thread
                }

                Thread.Sleep(spec.messageReadTimerDelay);
            }

            SyncReturns.TryRemove(WatchedGuid, out object ret);

            logger.Info("GetValue:Returned -> " + type);
            //spec.OnSyncedMessageEnd(null);
            spec.Connector.hub.QueueMessage(new NetCoreAdvancedMessage("{EVENT_SYNCEDMESSAGEEND}"));
            return(ret);
        }