Windows服务帮助'@Update' [英] Windows Service Help '@ Update'

查看:83
本文介绍了Windows服务帮助'@Update'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在为每天下午4:00创建弹出/'MessageBox'的简单Windows服务编写代码时遇到问题。我刚从大学毕业,在那里我主要学习数据库相关的东西。我已经完成了Windows服务,但我不知道从哪里开始计划事件。我花了很多时间寻找例子,我得到了一些但是现在对我来说它们太复杂了。



任何人都有机会为我做一个简单的例子吗?或者至少可以告诉我应该从哪里开始。 (我现在觉得自己像个白痴)



更新:

有一点问题,windows服务不会留下来。它只运行一次后发出Service has stopped..etc消息。它也不会创建新的日志文件。使用.NET 4进行第一次编码,所以事情看起来有点不同。



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();
		}
	}







谢谢,

MB




Thank you,
MB

推荐答案

看看 Windows服务中的计时器 [ ^ ]。



至少应该让你入门。



BTW:你永远不会猜到我用过的搜索词是什么。 :)
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. :)


您已经被告知了有助于您安排部分的计时器。要显示MessageBox,请不要通过Windows服务。 Windows服务不应该有任何用户界面。



您可以从服务中启动新的应用程序(Windows窗体应用程序)。要启动其他应用程序,请使用Process类。
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.


Windows服务通常不与桌面交互,主要是因为当没有人登录到该框时,服务可以运行。如果你需要每天下午4点弹出一些东西,我会编写一个应用程序并将其添加到Windows调度程序中。



至于在Windows服务中按计划执行某项操作,您需要设置一个坐在/旋转的线程,直到所需的日期/时间,如下所示:



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:

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.


这篇关于Windows服务帮助'@Update'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆