I have 4+ experience and working as software Engineer with Hytech professionals India PVT ltd and also working as Consultant,Trainer,Blogger and Author on Microsoft technologies,Also I have Trained more than 500+ IT professionals.I have knowledge about Asp.net,C#,SQL,Project management,replication,MVC,WebApi,SSRS,PostMan,TFS,SVN,Swagger,WMI,Agile,SignalR,Angularjs,IIS configuration.

Part 24::How to create task scheduler in C# ?

Today I will clarify about task scheduler in c# and how to scheduled on specific time  in window.Today i will create a task scheduler using c# console application to call or consume rest web api with proxy to get data from sharepoint .This example use to consume web api to call sharepoint to download document  on local application directory.

How to create Task scheduler in c# application?
Step 1:-Create a console application in c#
Step 2:-You need to add Microsoft.SharePoint.Client library from nugget package in you application.
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Configuration;
using System.Security;
using Microsoft.SharePoint.Client;
namespace InsightTaskSchedular{
    class Program    {
        static void Main()
            try            {
//to authenticate from sharepoint server.
                string UserName = ConfigurationSettings.AppSettings["UserName"].ToString();
                string Password = ConfigurationSettings.AppSettings["Password"].ToString();
                string userName = UserName;
                string password = Password;
                string baseurl = "***********Sharepoint URL"; 
                Uri uri = new Uri(baseurl);
                var securePassword = new SecureString();
                foreach (var c in password) { securePassword.AppendChar(c); }
                var credentials = new SharePointOnlineCredentials(userName, securePassword);
//get authCookie  from sharepoint using username and password.
                var authCookie = credentials.GetAuthenticationCookie(uri);
                string fedAuthString = authCookie.TrimStart("SPOIDCRL=".ToCharArray());
                if (fedAuthString != null)
//call function to consume web api using fedAuthString as token for api.
                else                {
                    Console.WriteLine("Invalid User!!");
            catch (TaskCanceledException ex)
        static async Task RunAsync(string fedAuthString)
            try            {
                using (HttpClient _client = new HttpClient())
//By default httpclient timeout=100 second.
                    _client.Timeout = TimeSpan.FromHours(1);
                    string BaseApiUrl = ConfigurationSettings.AppSettings["BaseApiUrl"].ToString();
                    _client.BaseAddress = new Uri(BaseApiUrl);
                    _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    _client.DefaultRequestHeaders.Add("SPOIDCRL", fedAuthString);
                    HttpResponseMessage response = await _client.GetAsync("/api/Insight/DownloadDocument"); 
                    if (response.IsSuccessStatusCode)
            catch (TaskCanceledException ex)
Step 3:-Set proxy and userName and password  in app.config file.

<?xml version="1.0" encoding="utf-8" ?>
      <proxy proxyaddress="proxy IP:port" usesystemdefault="true" bypassonlocal="true" />
    <add key="UserName" value="***********"/>
    <add key="Password" value="************"/>
    <add key="BaseApiUrl" value="***********:Port"/>    
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />

Step 3:-You can change this code according your requirment and run console application.
thereafter check bin folder your application directory to execute .exe  on anywhere.

How to use task scheduler in window operating system?
Step 1:-Open Task scheduler using these steps.
Open commend prompt=> type "Taskschd.msc" thereafter open window like this
Main window

Step 2:-Create new task in task scheduler using these step given below.
Task Scheduler Library=>right click=>Create Task  thereafter open window like this and set task Name and description and other configuration according your requirment .
General window

Step 3:-Now you will create trigger to schedule your task according your requirment like this.
trigger window

Step 4:-Now you will create actions for .exe. click on Browse and set path of exe file .
e.g.  ...\InsightTaskSchedular\bin\Debug\InsightTaskSchedular.exe
Action window

Step 5:-Now you can set other setting according your requirment as well as other.
and click on "OK".Thereafter you can check your task in task panel
Your task will run at scheduled time exactly. 

You have just read an article that categorized by title C# by title Part 24::How to create task scheduler in C# ?. You can bookmark this page with a URL https://bikeshsrivastava.blogspot.com/2016/07/part-24how-to-create-task-scheduler-in-c.html. Thank You!
Author: Bikesh Srivastava - Friday, 1 July 2016

2 comments to "Part 24::How to create task scheduler in C# ?"

Life Is Complicated, But Now programmer Can Keep It Simple.