Click here to Skip to main content
15,903,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I'm having trouble writing code for a simple windows service that creates a pop up/'MessageBox' @ 4:00pm every day. I'm fresh out of college, where I mostly learned database related stuff. I'm done with the windows service, but I have no idea where to start with the schedule event. I've spent a good part of the day looking for examples, I got a few ones but they're all too complex at the moment for me.

Any chance anyone has a simple example for me? Or at least can tell me where I should start. (I feel like an idiot now)

UPDATE:
Having a bit of a problem, windows service won't stay on. It only gives a message 'Service has stopped..etc' after it runs once. It also doesn't create the new log file. 1st time coding with .NET 4, so things look a bit different.

public class PortionPopUp : ServiceBase
	{
		public const string MyServiceName = "PortionPopUp";
		Thread pop_thread = null;
		
		public PortionPopUp()
		{
			InitializeComponent();
		}
		
		public static void main()
		{
			//Starts the PopUp Service.
			System.ServiceProcess.ServiceBase[] ServicesToRun;
			ServicesToRun = new System.ServiceProcess.ServiceBase[]
			{
				new PortionPopUp ()
			};
			ServiceBase.Run(ServicesToRun);
		}
		
		private void InitializeComponent()
		{
			this.ServiceName = MyServiceName;
		}
		
		protected override void Dispose(bool disposing)
		{
			// TODO: Add cleanup code here (if required)
			base.Dispose(disposing);
		}
		
		protected override void OnStart(string[] args)
		{
			// Writes to log file -- see WriteToFile
			// Checks if thread is running
			WriteToFile("Starting service...");
			if(pop_thread != null && pop_thread.IsAlive)
			{
				pop_thread.Abort();
				pop_thread = null;
				//kills thread
			}
			
			pop_thread = new Thread(new ThreadStart(ThreadProc));
			pop_thread.Start();
			//assigns and starts thread
		}
		
		protected override void OnStop()
		{
			// TODO: Add tear-down code here (if required) to stop your service.
			WriteToFile("Stopping service...");
			pop_thread.Abort();
		}
		
		protected void ThreadProc()
		{
			DateTime temp = DateTime.Now;
			DateTime nextRun = new DateTime (temp.Year, temp.Month, temp.Day, 16, 30, 0);
			while (true)
			{
				DateTime current = DateTime.Now;
				if(current>=nextRun)
				{
					//ADD CODE FOR RUNNING EXTERNAL FORM APP
					//TEMP CODE
					// ********
					WriteToFile("IT IS 4:00PM");
					
					// ********
					nextRun.AddDays(1);
				}
				Thread.Sleep(2000);
			}
		}
		
		protected void WriteToFile (string msg)
		{
			FileStream fs = new FileStream(@"c:\PortionPopUp\ss_time.log", FileMode.OpenOrCreate, FileAccess.Write);
			StreamWriter sw = new StreamWriter(fs);
			sw.BaseStream.Seek(0,SeekOrigin.End);
			msg = DateTime.Now.ToString() + ": " + msg;
			sw.WriteLine("PortionPopUp {0}", msg);
			sw.Flush();
			fs.Close();
		}
	}



Thank you,
MB
Posted
Updated 2-Feb-11 20:04pm
v4

Take a look at Timer in Windows Service[^].

Should get you started at least.

BTW: You'll never guess what the search phrase I used was. :)
 
Share this answer
 
You have already been told about the timer that will help you with the scheduling part. For showing MessageBox, do not do it through the windows service. Windows service is not supposed to have any user interface.

You can start a new application from within your service (a windows form application). To start the other application, use Process class.
 
Share this answer
 
Comments
#realJSOP 2-Feb-11 8:10am    
In order to launch a GUI app, you have to set the service to interact with the desktop. It's generally not a good idea. TRHere are other methods he can use.
Windows services don't typically interact with the desktop, mostly because a service can run when nobody is logged onto the box. If you need something to pop up at 4pm every day, I would write an application and add it to the Windows scheduler.

As far as doing something on a schedule in a Windows service, you need to set up a thread that sits/spins until the desired date/time, something like this:

C#
public class MyService
{
    Thread m_thread = null;

    private void OnStart(string[] args)
    {
        if (m_thread != null && m_thread.State == ThreadState.Running)
        {
            m_thread.Abort();
            m_thread = null;
        }
        m_thread = new Thread(new ThreadStart(ThreadProc));
        m_thread.Start();
    }

    private void OnStop()
    {
        m_thread.Abort();
    }

    private void ThreadProc()
    {
        DateTime temp = DateTime.Now;
        DateTime nextTime = new DateTime(temp.Year, temp.Month, temp.Day, 16, 0, 0, 0);
        while (true)
        {
            DateTime now = DateTime.Now;
            if (now >= nextTime)
            {
                // do something 
                nextTime.AddDays(1);
            }
            Sleep(750);
        }
    }
}


The code above may need some tweaking as I did it off the top of my head.
 
Share this answer
 
v3
You can show a non-blocking Message Box from your Windows Service to the User. For this, firstly, start a Service named "Interactive Service Detection" from "services.msc". Then initiate a Thread from your Windows Service. In the thread method use "MessageBox.Show()" method to display your Message box to user. Instead of manually starting "Interactive Service Detection" service you can start it C# program.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900