Results 1 to 4 of 4

Thread: [RESOLVED] [1.0/1.1] Stopping a Service

  1. #1

    Thread Starter
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610

    Resolved [RESOLVED] [1.0/1.1] Stopping a Service

    Simple question me thinks...

    When OnStop() is called from the service manager how do I tell the thread that was created in OnStart() that it needs stop doing what it is doing?

    Another question is, if for some reason I cannot stop the thread yet, is there a safe way to let the service mangager know this and I will shutdown the thread when I can?

    I have the following basic service class:
    Code:
    public class Replication : System.ServiceProcess.ServiceBase
    {
    	static ReplicationMain replicationService = new ReplicationMain();
    
    	protected override void OnStart(string[] args)
    	{
    		Thread workerThread = new Thread(new ThreadStart(replicationService.Start));
    		workerThread.Start();
    		this.isStopped = true;
    	}
    	protected override void OnStop()
    	{
    	// How to tell replicationService we are shutting down
    	}
    }
    Code:
    public class ReplicationMain
    {
    	public void Start()
    	{
    	}
    }
    So once I am in the ReplicationMain class I was not sure how I can get a message from the Replication class. I hope that made sense.

    Steve
    This space for rent...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [1.0/1.1] Stopping a Service

    There are basically two ways to stop a thread. You can call its Abort method, which throws a ThreadAbortException on that thread. You handle the exception and perform whatever cleanup is required. Alternatively you can set a flag to indicate to the thread that it should end. The thread will test this flag at reasonable intervals and if it is set then the thread will clean up and exit.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610

    Re: [1.0/1.1] Stopping a Service

    Thank you for the reply. I have not done much threading yet, I was not sure if it was safe to access a flag in a class once it has been threaded, so to speak.

    So the code below would be safe and all I need to do is check for the value of m_flag, correct?

    Code:
    public class Replication : System.ServiceProcess.ServiceBase
    {
    	static ReplicationMain replicationService = new ReplicationMain();
    
    	protected override void OnStart(string[] args)
    	{
    		Thread workerThread = new Thread(new ThreadStart(replicationService.Start));
    		workerThread.Start();
    		this.isStopped = true;
    	}
    	protected override void OnStop()
    	{
    	replicationService.Stop(true);
    	}
    }
    
    public class ReplicationMain
    {
    	public void Stop(bool flag)
    	{
    		m_flag = flag;
    	}
    }
    This space for rent...

  4. #4

    Thread Starter
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610

    Re: [1.0/1.1] Stopping a Service

    Hi all,

    I have finally been able to take a closer look at this. This is what I came up with to stop my service.

    When the service runs it will end up looping through a process as much as a 1000 times. Each loop could take about a minute or two, to process. So I was planning on sending information to the loop that the service is trying to stop. Then in the OnStop method I am looping until the class tells me that it has completed a process.

    Is this a good solution to this? I am concerned that I may not be handling the threading correctly

    Thanks Steve
    Code:
    public class NewService : System.ServiceProcess.ServiceBase
    {
    	private System.Timers.Timer	_serviceTime = new System.Timers.Timer();
    	private NewClass 		_newClassService = new NewClass();
    
    	static void Main()
    	{
    		NewService newSampleService = new NewService();
    		newSampleService.Start();
    	}
    
    	protected override void OnStart(string[] args)
    	{
    		Thread t = new Thread(new ThreadStart(Start));
    		t.Start();
    	}
    
    	private void Start()
    	{
    		_serviceTime.Enabled = true;
    	}
    
    	private void OnServiceElapsedTime(object source, System.Timers.ElapsedEventArgs e)
    	{
    		_newClassService.Start();
    	}
    
    	protected override void OnStop()
    	{
    		//This will change a flag in the class to tell in to stop if the service is processing
    		_newClassService.Stop();
    	}
    }
    
    public class NewClass
    {
    	private bool			_stopService = false;
    	private bool			_processStopped = true;
    
    	public void Start()
    	{
    		while(_stopProcess == false)
    		{
    			_processStopped = false;
    			// Start doing what I need
    		}
    		_processStopped = true;
    	}
    	public void Stop()
    	{
    		_stopService = true;
    		while (_processStopped == false){}
    	}
    }
    This space for rent...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width