This post show how to create a simple task scheduler in C#.The program takes input(hours,minutes) from the user and display a message box at a specific time(Input Time).Create a new C# Windows Form Project in Visual studio.
Output:
Download Source Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TaskScheduler
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
try
{
DateTime dtfirstdate = DateTime.Now;
string dtDate = dtfirstdate.ToShortDateString();
int ihours = Convert.ToInt32(textBox1.Text);
int iminutes = Convert.ToInt32(textBox2.Text);
string[] dtarray = dtDate.Split('-');
DateTime dtSecondDate = new DateTime(Convert.ToInt32(dtarray[2]),Convert.ToInt32(dtarray[1]),Convert.ToInt32(dtarray[0]),ihours,iminutes,00);
TimeSpan tsdifference = dtSecondDate - dtfirstdate;
Task.Delay(tsdifference).Wait();
MessageBox.Show("Task executed at difference of..."+tsdifference.ToString());
}
catch (Exception ex)
{
MessageBox.Show("Exception found..."+ex.ToString());
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TaskScheduler
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
try
{
DateTime dtfirstdate = DateTime.Now;
string dtDate = dtfirstdate.ToShortDateString();
int ihours = Convert.ToInt32(textBox1.Text);
int iminutes = Convert.ToInt32(textBox2.Text);
string[] dtarray = dtDate.Split('-');
DateTime dtSecondDate = new DateTime(Convert.ToInt32(dtarray[2]),Convert.ToInt32(dtarray[1]),Convert.ToInt32(dtarray[0]),ihours,iminutes,00);
TimeSpan tsdifference = dtSecondDate - dtfirstdate;
Task.Delay(tsdifference).Wait();
MessageBox.Show("Task executed at difference of..."+tsdifference.ToString());
}
catch (Exception ex)
{
MessageBox.Show("Exception found..."+ex.ToString());
}
}
}
}
Output:
No comments:
Post a Comment