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.
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.
RunAsync(fedAuthString).Wait(); //Console.WriteLine(fedAuthString);
//Console.Read();
} else { Console.WriteLine("Invalid User!!"); } } catch (TaskCanceledException ex) { Console.WriteLine(ex); Console.Read(); } } 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.Clear(); _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); _client.DefaultRequestHeaders.Add("SPOIDCRL", fedAuthString); HttpResponseMessage response = await _client.GetAsync("/api/Insight/DownloadDocument"); if (response.IsSuccessStatusCode) { Console.WriteLine("Sucess!!"); } } } catch (TaskCanceledException ex) { Console.WriteLine(ex); Console.Read(); } } } }
Step 3:-Set proxy and userName and password in app.config file.
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.net> <defaultProxy> <proxy proxyaddress="proxy IP:port" usesystemdefault="true" bypassonlocal="true" /> </defaultProxy> </system.net> <appSettings> <add key="UserName" value="***********"/> <add key="Password" value="************"/> <add key="BaseApiUrl" value="***********:Port"/> </appSettings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> </startup> </configuration>
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
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 .
Step 3:-Now you will create trigger to schedule your task according your requirment like this.
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
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 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, July 1, 2016
Very well explained!! Really helpful..
ReplyDeleteThanks
DeleteI like your blog, I read this blog please update more content on hacking,
ReplyDeleteNice post,and good information Thanks for sharing
further check it once at Dot NET Online Course Hyderabad