And now for the stuff that topic is all about: Starting custom timer job programmatically (or by code, all about right words). Let’s start from the point where you have done a Custom Timer Job (there’s now plenty of stuff from this topic). And now you want to start it programmatically, on demand. This topic is not so much talked about but after a trial and error I found a way to do this from code.
Web Part side (like a button event or so, where you want to start the Timer Job):
SPSite site = SPContext.Current.Site;
string timerJobName = "HBExportJob";
CustomTimerJob myCustomTimerJob= new CustomTimerJob (timerJobName, site.WebApplication);
SPOneTimeSchedule schedule = new SPOneTimeSchedule(DateTime.Now);
myCustomTimerJob.Schedule = schedule;
CustomTimerJob is a class that is inheritated from SPJobDefinition.
That code kicks the timer job running programmatically. I began this process by creating a custom timer job and then I looked about the common installer classes from examples that are in the Internet. Then it hit like a ligthning from a clear sky, you can bypass that installer and put actual logic class with your webpart and start that timer job. Also you could iterate through all SPJobDefinitions and find your Timer Job in that SPWebApplication.JobDefinitions and start from there programmatically.
I hope that someone else gets a bright ideas like this. Now when I look at this I can say that this is not much but when you go all the way through the process of thinking, trying and failing and trying again, the feeling is something very different.. Btw I also had a code that actually went through SPWebApplication.JobDefinitions and added the timer job deep in there and executed the timer job but it led me having synchronous timer job with web part. Not nice in this case but possible 😉
Okay btw. I am sorry for the baaad english 😉