如何创建任务计划程序应用程序
我一直在创建一个应用程序,该应用程序将允许用户使用参数来安排我们拥有的命令行应用程序。
所以命令行应用程序需要一个xml并“运行它”
所以底线我要么需要创建一个Windows服务,要么学习如何与已经在盒子上运行的任务计划程序服务交互(版本1 Xp / 2003)
起初我想运行一个服务很容易,当提交作业时,计算从现在到运行之间的时间,并设置一个计时器来等待该时间。这比每分钟检查是否到了运行时间要好。
如果我碰壁了,我意识到我不知道如何与正在运行的 Windows 服务进行通信。除非创建一个包含详细信息的文件,并使用带有文件观察器的服务来加载文件并修改计划。
因此,根本问题是我如何
从客户端
serviceThatIsRunning.Add(Job)
执行此 psedo 代码,或者与任务计划交互或使用 c# 3.5 创建 .job 文件
编辑:
为了澄清,我创建了一个小示例来了解我对“的想法”所以
我有一个作业类
public class Job
{
#region Properties
public string JobName { get; set; }
public string JobXML { get; set; }
private Timer _JobTimer;
public Timer JobTimer
{
get
{
return _JobTimer;
}
}
#endregion
public void SetJobTimer(TimeSpan time)
{
if (_JobTimer != null)
{
_JobTimer.Dispose();
}
_JobTimer = new Timer(new TimerCallback(RunJob), null, time, time);
}
private void RunJob(Object state)
{
System.Diagnostics.Debug.WriteLine(String.Format("The {0} Job would have ran with file {1}", JobName, JobXML));
}
public override string ToString()
{
return JobName;
}
public void StopTimer()
{
_JobTimer.Dispose();
}
}
现在我需要创建一个应用程序来容纳这些不断运行的作业,这就是为什么我想到 Windows 服务,然后是一个 Windows 应用程序来允许用户使用作业列表。
所以问题是,如果我创建一个 Windows 服务,我如何与该服务中的方法交互,以便我可以更改 JobList、添加、删除、更改。
这是我创建的一个小型 Windows 应用程序,用于显示 Job 类确实运行。有趣的一点是,如果我正确执行此操作,我不会将作业添加到列表框,并且 Add 方法退出时作业计时器部分仍然运行并且不会被垃圾收集器拾取。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnAddJob_Click(object sender, EventArgs e)
{
Job job = new Job();
job.JobName = txtJobName.Text;
job.JobXML = txtJobXML.Text;
job.SetJobTimer(new TimeSpan(0, 0, Convert.ToInt32(JobTime.Value)));
// ??Even If I don't add the Job to a list or ListBox it seems
// ??to stay alive and not picked up by the GC
listBox1.Items.Add(job);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex > -1)
{
Job job = listBox1.Items[listBox1.SelectedIndex] as Job;
txtJobName.Text = job.JobName;
txtJobXML.Text = job.JobXML;
}
}
private void btnRemove_Click(object sender, EventArgs e)
{
Job job = listBox1.Items[listBox1.SelectedIndex] as Job;
job.StopTimer();
listBox1.Items.Remove(job);
}
private void btnCollect_Click(object sender, EventArgs e)
{
GC.Collect();
}
}
I have been task with (ha) creating an application that will allow the users to schedule a command line app we have with a parameter.
So the command line app takes an xml and "runs it"
So bottom line I either need to create a windows service or learn how to interact with the Task Scheduler service already running on the box (version 1 Xp /2003)
At first I though it would be easy have a service run and when a job is submitted, calculate the time between now and run and set up a timer to wait that amount of time. This is better then checking every minute if it's time to run.
Were I hit a wall is I relized I do not know how to communicate with a running windows service. Except maybe create a file with details and have the service with a file watcher to load the file and modify the schedule.
So the underlying questions are how can I execute this psedo code
from client
serviceThatIsRunning.Add(Job)
Or ineracting with the task schedule or creating .job files using c# 3.5
Edit:
To clarify I created a small sample to get my thoughts on "paper"
So I have a Job Class
public class Job
{
#region Properties
public string JobName { get; set; }
public string JobXML { get; set; }
private Timer _JobTimer;
public Timer JobTimer
{
get
{
return _JobTimer;
}
}
#endregion
public void SetJobTimer(TimeSpan time)
{
if (_JobTimer != null)
{
_JobTimer.Dispose();
}
_JobTimer = new Timer(new TimerCallback(RunJob), null, time, time);
}
private void RunJob(Object state)
{
System.Diagnostics.Debug.WriteLine(String.Format("The {0} Job would have ran with file {1}", JobName, JobXML));
}
public override string ToString()
{
return JobName;
}
public void StopTimer()
{
_JobTimer.Dispose();
}
}
Now I need to create an App to house these Jobs that is constantly running, that is why I though of Windows Services, and then a Windows app to allow the user to work with the Job List.
So the question is if I create a Windows Service how do I interact with methods in that service so I can change the JobList, add, delete, change.
Here is a small windows app I created to show that the Job class does run. Interesting point, If I am doing this correctly, I do not add the Job to a listbox and the Add method exits the Job Timer portion still runs and does not get picked up by the Garbage Collector.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnAddJob_Click(object sender, EventArgs e)
{
Job job = new Job();
job.JobName = txtJobName.Text;
job.JobXML = txtJobXML.Text;
job.SetJobTimer(new TimeSpan(0, 0, Convert.ToInt32(JobTime.Value)));
// ??Even If I don't add the Job to a list or ListBox it seems
// ??to stay alive and not picked up by the GC
listBox1.Items.Add(job);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex > -1)
{
Job job = listBox1.Items[listBox1.SelectedIndex] as Job;
txtJobName.Text = job.JobName;
txtJobXML.Text = job.JobXML;
}
}
private void btnRemove_Click(object sender, EventArgs e)
{
Job job = listBox1.Items[listBox1.SelectedIndex] as Job;
job.StopTimer();
listBox1.Items.Remove(job);
}
private void btnCollect_Click(object sender, EventArgs e)
{
GC.Collect();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想使用任务计划程序来计划任务,可能会像下面这样简单。您只需根据需要自定义传递给 schtasks 的命令行参数即可。请参阅此链接< /a> 有关命令行参数的详细说明。
If you want to schedule a task using the task scheduler it could be as simple as below. You just need to customize the command line arguments that you pass to
schtasks
for your needs. See this link for a detailed explanation of command line arguments.如果您想启动以不同时间间隔运行的多个任务,您可以
例如,创建一个 JobThread 类,它定义一个使用 Initialize 方法初始化的计时器:
m_timer = new Timer(new TimerCallback(this.timerHandler), null, this.Interval, this.Interval);
此外,此类定义了 Job 对象的列表。这些作业是从timerHandler 执行的。
最后,您创建一个定义 Start 和 Stop 方法的单例 JobManager 类。
在 Start 方法中,您执行如下操作:
此 JobManager 还有一个接受 XmlNode 参数的 Initiliaze 方法。此方法将解析您从命令行传递的 Xml 作业。
If you want to start multiple tasks that run at different time intervals, you can
create for instance a class JobThread that defines a timer that is initialized using the Initialize method:
m_timer = new Timer(new TimerCallback(this.timerHandler), null, this.Interval, this.Interval);
Furthermore, this class defines a List of Job objects. These jobs are executed from the timerHandler.
Finally, you create a singleton JobManager class that defines a Start and Stop method.
In the Start method you do something like this:
This JobManager has also a Initiliaze method that accepts a XmlNode parameter. This method will parse the Xml-job you pass from the command-line.
该线程上有一个答案不再存在,但是,我将尝试通过
Windows 服务保持端口打开 WCF 来创建侦听器
http://msdn.microsoft.com/en-us/library/ms733069。 aspx
还添加属性
有助于保持服务状态。
There was an answer on this thread that is no longer there but, I am going to try to create a listener by keeping a port open
WCF through Windows Services
http://msdn.microsoft.com/en-us/library/ms733069.aspx
Also adding the attribute
Helps to keep state of the service.