0

I'm doing some masks for SAP B1 using c#.

I'd need to know how to create a function that, automatically (for examples every 15 minutes), take some data and put its on a database. The function is already done but how can I create the automatic execution in background?

Best regards and thanks in advance for the reply, Lorenzo

Loll
  • 11
  • 1
  • Use a timer of some kind, or run your program as a scheduled task. – Equalsk Jan 27 '17 at 16:21
  • 1
    For as simple as your implementation sounds from the question I would recommend using windows scheduled tasks,. – Theo Jan 27 '17 at 16:23
  • It depends: does this application more resemble a script, i.e. it runs, does a discrete job or test of sub-tasks, and then stops? If so, I would go with the scheduled task recommendation. If this is an application, like a daemon, that should always be running even if it's just cycling and doing the same thing, then I would go with a timer. Remember, a scheduled task will result in a different process ID each time it runs, whereas a daemon with a timer will have the same process ID it was first started with. That may or may not matter to you. – rory.ap Jan 27 '17 at 16:40
  • Possible duplicate of [calling method on every x minutes](http://stackoverflow.com/questions/13019433/calling-method-on-every-x-minutes) – ivan_pozdeev Jan 28 '17 at 02:22

2 Answers2

4

Timer is what you need:

var timer = new System.Threading.Timer(
    e => Method(),  
    null, 
    TimeSpan.Zero, 
    TimeSpan.FromMinutes(15));

This will call Method() every 15 minutes.

Timer info : https://msdn.microsoft.com/en-us/library/system.threading.timer.aspx

Ambrish Pathak
  • 3,813
  • 2
  • 15
  • 30
0

You could use Timer like @AmbroishPathak suggested. Also, as mentioned in the comments, you can use the Windows Task Scheduler to run your script or executable. The advantage of this is that the process won't be running in the background while it's not doing work.

You can see the details of how to schedule a task here.

The following answer describes this as well: windows scheduler to run a task every x-minutes?

To summarize the accepted answer there, you create the task to run once a day. After that, you can double-click on the task to bring up its Properties window and go to the "Triggers" tab. Under "Advanced Settings" you should be able to set it to run every x number of minutes.

Community
  • 1
  • 1