Example #1
0
        /// <summary>
        /// Start work
        /// </summary>
        public void Start(ManualResetEvent externalResetEvent)
        {
            _externalResetEvent = externalResetEvent ?? throw new ArgumentNullException("manualResetEvent");

            try
            {
                _worker      = new Thread(new ThreadStart(ThreadFunction));
                _worker.Name = _threadName;
                _worker.Start();

                Thread.Sleep(0);
                ServiceState = ServiceStateEnum.Running;
            }
            catch
            {
                try
                {
                    Stop();
                    ServiceState = ServiceStateEnum.Stopped;
                }
                catch { } // We really have to stop somewhere
                throw;
            }

            Log.Information("RealWork is running now.");
        }
            // report status to Service Control Manager window
            protected void SetStatus(ServiceStateEnum newStatus)
            {
                ServiceStatus serviceStatus = new ServiceStatus()
                {
                    dwCurrentState = newStatus,
                    dwWaitHint     = 100000,
                };

                SetServiceStatus(svc.ServiceHandle, ref serviceStatus);
            }
Example #3
0
        /// <summary>
        /// C'tor
        /// </summary>
        public RealWork( )
        {
            // Set internal reset event to non-signalled
            _internalResetEvent.Reset();
            _worker = null;

            _threadName    = ConfigurationHelper.Instance.GetConfigurationValue("Thread: Name") ?? "RealWork";
            _scanFrequency = int.Parse(
                ConfigurationHelper.Instance.GetConfigurationValue("Thread:ScanFrequencyInMilliseconds") ?? "10000");

            ServiceState = ServiceStateEnum.Stopped;
        }
        private void StopServerAsync(object nothing)
        {
            lock (_syncLock)
            {
                DicomServer server = _server;
                _server = null;

                if (server != null)
                {
                    Trace.WriteLine("Stopping Dicom server.");
                    server.Stop();
                }

                _serviceState = ServiceStateEnum.Stopped;
                OnServerStopped();
            }
        }
        private void StopServer(bool wait)
        {
            lock (_syncLock)
            {
                if (_serviceState == ServiceStateEnum.Started)
                {
                    _serviceState = ServiceStateEnum.Stopping;
                    ThreadPool.QueueUserWorkItem(StopServerAsync);
                }

                if (wait)
                {
                    while (_serviceState != ServiceStateEnum.Stopped)
                    {
                        Monitor.Wait(_syncLock, 50);
                    }
                }
            }
        }
Example #6
0
        /// <summary>
        /// Stop work
        /// </summary>
        public void Stop()
        {
            // Do some cleanup here, depending on what specifically you are doing
            _internalResetEvent.Set();
            Log.Information("In RealWork.Stop(), received signal to stop.");

            try
            {
                try
                {
                    // Big question here...is thread in blocking or executing state?
                    if ((_worker.ThreadState & ThreadState.WaitSleepJoin) == 0)
                    {
                        _worker.Interrupt();
                    }
                    else
                    {
                        _worker.Abort();
                        _worker.Join(20);
                    }
                }
                catch (ThreadInterruptedException)
                {
                    ServiceState = ServiceStateEnum.Dead;

                    try
                    {
                        _worker.Abort();
                        _worker.Join(20);
                    }
                    catch { }
                }
            }
            catch
            {
                // We should really never get to here
                ServiceState = ServiceStateEnum.Runaway;
                throw;
            }
        }
Example #7
0
        public ServiceState(string state)
        {
            switch (state)
            {
            case "Stopped":
                _state = ServiceStateEnum.Stopped;
                break;

            case "Start Pending":
                _state = ServiceStateEnum.StartPending;
                break;

            case "Stop Pending":
                _state = ServiceStateEnum.StopPending;
                break;

            case "Running":
                _state = ServiceStateEnum.Running;
                break;

            case "Continue Pending":
                _state = ServiceStateEnum.ContinuePending;
                break;

            case "Pause Pending":
                _state = ServiceStateEnum.PausePending;
                break;

            case "Paused":
                _state = ServiceStateEnum.Paused;
                break;

            default:
                _state = ServiceStateEnum.Unknown;
                break;
            }
        }
        private void StartServerAsync(object nothing)
        {
            DicomServerConfiguration serverConfiguration;

            lock (_syncLock)
            {
                serverConfiguration = Common.DicomServer.DicomServer.GetConfiguration();
                _restart            = false;
            }

            DicomServer server = null;

            try
            {
                Trace.WriteLine("Starting Dicom server.");

                server = new DicomServer(serverConfiguration);
                server.Start();
            }
            catch (Exception e)
            {
                Platform.Log(LogLevel.Error, e, "Failed to start dicom server ({0}/{1}:{2}",
                             serverConfiguration.HostName, serverConfiguration.AETitle, serverConfiguration.Port);

                server = null;
            }
            finally
            {
                lock (_syncLock)
                {
                    //the server may be null here, we are just reflecting the state based on the method calls.
                    _server       = server;
                    _serviceState = ServiceStateEnum.Started;
                    OnServerStarted();
                }
            }
        }
		private void StartServerAsync(object nothing)
		{
			DicomServerConfiguration serverConfiguration;
            lock (_syncLock)
            {
                serverConfiguration = Common.DicomServer.DicomServer.GetConfiguration();
				_restart = false;
			}

			DicomServer server = null;

			try
			{
				Trace.WriteLine("Starting Dicom server.");

				server = new DicomServer(serverConfiguration);
				server.Start();
			}
			catch (Exception e)
			{
				Platform.Log(LogLevel.Error, e, "Failed to start dicom server ({0}/{1}:{2}",
				             serverConfiguration.HostName, serverConfiguration.AETitle, serverConfiguration.Port);

				server = null;
			}
			finally
			{
				lock (_syncLock)
				{
					//the server may be null here, we are just reflecting the state based on the method calls.
					_server = server;
					_serviceState = ServiceStateEnum.Started;
					OnServerStarted();
				}
			}
		}
 public static void Reset()
 {
     ServiceState = ServiceStateEnum.Stopped;
 }
 public void PausePending()
 {
     status = ServiceStateEnum.SERVICE_PAUSE_PENDING;
     SetStatus(status);
 }
 public void ContinuePending()
 {
     status = ServiceStateEnum.SERVICE_CONTINUE_PENDING;
     SetStatus(status);
 }
 public RestartListenerResult RestartListener(RestartListenerRequest request)
 {
     ServiceState = ServiceStateEnum.Started;
     return new RestartListenerResult();
 }
 public void Stopped()
 {
     status = ServiceStateEnum.SERVICE_STOPPED;
     SetStatus(status);
 }
		private void StopServerAsync(object nothing)
		{
			lock (_syncLock)
			{
				DicomServer server = _server;
				_server = null;

				if (server != null)
				{
					Trace.WriteLine("Stopping Dicom server.");
					server.Stop();
				}

				_serviceState = ServiceStateEnum.Stopped;
				OnServerStopped();
			}
		}
		private void StopServer(bool wait)
		{
			lock (_syncLock)
			{
				if (_serviceState == ServiceStateEnum.Started)
				{
					_serviceState = ServiceStateEnum.Stopping;
					ThreadPool.QueueUserWorkItem(StopServerAsync);
				}

				if (wait)
				{
					while (_serviceState != ServiceStateEnum.Stopped)
						Monitor.Wait(_syncLock, 50);
				}
			}
		}
 public MultithreadingServices()
 {
     _state = ServiceStateEnum.Initialize;
 }
        private void ServiceStart()
        {
            _serviceFlag = true;
            _state = ServiceStateEnum.Runing;
            //_threadStart = new ThreadStart(RunService);
            InitThreadPool();
            _log.Debug("MultithreadingServices Start");
            foreach (ThreadInformation t in _threadPools.Values)
            {
                t.Flag = true;
                if (t.StartTime.Equals(DateTime.MinValue))
                {
                    t.StartTime = DateTime.Now;
                }
                if (t.ProcessThread.ThreadState == ThreadState.Stopped ||
                    t.ProcessThread.ThreadState == ThreadState.Unstarted||
                    t.ProcessThread.ThreadState==ThreadState.Aborted)
                {
                    t.ProcessThread.Start();

                }
            }
        }
Example #19
0
 public ServiceState(ServiceStateEnum state)
 {
     _state = state;
 }
 public RestartListenerResult RestartListener(RestartListenerRequest request)
 {
     ServiceState = ServiceStateEnum.Started;
     return(new RestartListenerResult());
 }
 public static void Reset()
 {
     ServiceState = ServiceStateEnum.Stopped;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ServiceStateChangedEventArgs"/> class.
 /// </summary>
 /// <param name="state">The state.</param>
 public ServiceStateChangedEventArgs(ServiceStateEnum state)
 {
     this.ServiceState = state;
 }
 public void Paused()
 {
     status = ServiceStateEnum.SERVICE_PAUSED;
     SetStatus(status);
 }
 public void StopPending()
 {
     status = ServiceStateEnum.SERVICE_STOP_PENDING;
     SetStatus(status);
 }
 public void Running()
 {
     status = ServiceStateEnum.SERVICE_RUNNING;
     SetStatus(status);
 }
Example #26
0
 /// <summary>
 /// Called when [service state changed].
 /// </summary>
 /// <param name="state">The state.</param>
 protected virtual void OnServiceStateChanged(ServiceStateEnum state)
 {
     this.ServiceState = state;
     RaiseEvent(EventServiceStateChanged, this, new ServiceStateChangedEventArgs(state));
 }