private void FormProgressStatus_Shown(object sender, EventArgs e)
        {
            //Spawn a separate thread that will monitor if this progress form should has indicated that it needs to close.
            //This thread will be a fail-safe in the sense that it will constantly monitor a separate indicator that this window should close.
            ODThread threadForceCloseMonitor = new ODThread(100, new ODThread.WorkerDelegate((o) => {
                if (_hasClosed)
                {
                    o.QuitAsync();                    //Stop monitoring as soon as we detect that this window has been "closed".
                    return;
                }
                if (ForceClose)
                {
                    //Something triggered the fact that this form should have closed.
                    this.InvokeIfRequired(() => {
                        DialogResult = DialogResult.OK;
                        ODException.SwallowAnyException(() => Close()); //Can throw exceptions for many reasons.
                    });
                    o.QuitAsync();                                      //We tried our best to "unstuck" this window.  The user will have to Alt + F4 this window closed?
                    return;
                }
            }));

            threadForceCloseMonitor.AddExceptionHandler((ex) => {
                ex.DoNothing();                //The form might stay open forever which was already happening... we tried our best.
            });
            threadForceCloseMonitor.Name = "FormProgressStatusMonitor_" + DateTime.Now.Ticks;
            threadForceCloseMonitor.Start();
        }
Exemple #2
0
        ///<summary>Sends a requests to ODCloudClient and waits for a response. Throws any exception that ODCloudClient returns.</summary>
        private static string SendToODCloudClientSynchronously(ODCloudClientData cloudClientData, CloudClientAction cloudClientAction, int timeoutSecs = 30)
        {
            bool      hasReceivedResponse   = false;
            string    odCloudClientResponse = "";
            Exception exFromThread          = null;

            void onReceivedResponse(string response)
            {
                hasReceivedResponse   = true;
                odCloudClientResponse = response;
            }

            ODThread thread = new ODThread(o => {
                SendToODCloudClient(cloudClientData, cloudClientAction, onReceivedResponse);
            });

            thread.Name = "SendToODCloudClient";
            thread.AddExceptionHandler(e => exFromThread = e);
            thread.Start();
            DateTime start = DateTime.Now;

            ODProgress.ShowAction(() => {
                while (!hasReceivedResponse && exFromThread == null && (DateTime.Now - start).TotalSeconds < timeoutSecs)
                {
                    Thread.Sleep(100);
                }
            });
            if (exFromThread != null)
            {
                throw exFromThread;
            }
            if (!hasReceivedResponse)
            {
                throw new ODException("Unable to communicate with OD Cloud Client.", ODException.ErrorCodes.ODCloudClientTimeout);
            }
            CloudClientResult result = JsonConvert.DeserializeObject <CloudClientResult>(odCloudClientResponse);

            if (result.ResultCodeEnum == CloudClientResultCode.ODException)
            {
                ODException odEx = JsonConvert.DeserializeObject <ODException>(result.ResultData);
                throw odEx;
            }
            if (result.ResultCodeEnum == CloudClientResultCode.Error)
            {
                throw new Exception(result.ResultData);
            }
            return(result.ResultData);
        }
        public FormProgressBase(ODEventType odEventType, Type eventType)
        {
            this.FormClosing += new FormClosingEventHandler(this.FormProgressStatus_FormClosing);
            this.FormClosed  += new FormClosedEventHandler(this.FormProgressStatus_FormClosed);
            this.Shown       += new EventHandler(this.FormProgressStatus_Shown);
            if (eventType == null)
            {
                eventType = typeof(ODEvent);
            }
            _eventType   = eventType;
            _odEventType = odEventType;
            //Registers this form for any progress status updates that happen throughout the entire program.
            ODException.SwallowAnyException(() => _eventInfoFired = _eventType.GetEvent("Fired"));
            if (_eventInfoFired == null)
            {
                throw new ApplicationException("The 'eventType' passed into FormProgressStatus does not have a 'Fired' event.\r\n"
                                               + "Type passed in: " + eventType.GetType());
            }
            //Register the "Fired" event to the ODEvent_Fired delegate.
            Delegate   delegateFired    = Delegate.CreateDelegate(_eventInfoFired.EventHandlerType, this, "ODEvent_Fired");
            MethodInfo methodAddHandler = _eventInfoFired.GetAddMethod();

            methodAddHandler.Invoke(this, new object[] { delegateFired });
        }