Ejemplo n.º 1
0
        }         // proc Dispose

        #endregion

        #region -- Execute ----------------------------------------------------------------

        private void Execute()
        {
            var lastExceptionTick = 0;            // Wann wurde die Letzte Exeption abgefangen
            var exceptionCount    = 0;            // Wieviele wurde im letzten Zeitfenster gefangen

            // mark start of the thread
            log.Start(thread.Name);
            RegisterThread(this);
            try
            {
                startedEvent?.Set();
                do
                {
                    try
                    {
                        ExecuteLoop();
                    }
                    catch (ThreadAbortException)
                    {
                        log.Abort(Thread.CurrentThread.Name);
                        if (propertyRunning != null)
                        {
                            propertyRunning.Value = "Abgebrochen";
                        }
                        UnregisterThread();
                        return;
                    }
                    catch (Exception e)
                    {
                        if (!OnHandleException(e))
                        {
                            if (unchecked (Environment.TickCount - lastExceptionTick) > 500)
                            {
                                exceptionCount = 0;
                            }

                            lastExceptionTick = Environment.TickCount;
                            exceptionCount++;

                            if (exceptionCount > 3)
                            {
                                propertyRunning.Value = "Fehlerhaft";
                                Thread.CurrentThread.Abort();                                 // mehr als 3 Exeption in 1500 ms ist fatal
                            }
                            else
                            {
                                log.LogMsg(LogMsgType.Error,
                                           String.Format("Thread '{0}' neugestartet: {1}" + Environment.NewLine + Environment.NewLine +
                                                         "{2}", thread.Name, exceptionCount, e.GetMessageString()));
                                Thread.Sleep(100);
                            }
                        }
                        else
                        {
                            exceptionCount = 0;
                        }

                        propertyRestarts.Value += 1;
                    }
                } while (IsRunning);

                // stop the thread
                log.Stop(thread.Name);
                propertyRunning.Value = "Beendet";
                UnregisterThread(this);
            }
            catch (ThreadAbortException)
            {
                UnregisterThread(this);
            }
        }         // proc Execute