# Periodically sending data from database to an API

I'm trying to make a timed background service in .NET Core (I found this approach in Microsoft Docs), that every 15 seconds it gets data from the database (1000 rows max, if something exists) then sends it to the API using a foreach loop. If a response status code is not 200 or 202, I change thr status column by id in the database.

How can I make this code faster? Should I use another approach?

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Text;

namespace FDProcessor
{
class FDSenderService : IHostedService, IDisposable
{
private Timer timer;
private HttpClient httpClient = new HttpClient
{
};

{
Console.WriteLine("Service is started.");
timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(15));
}

private async void DoWork(object state)
{
var data = DbManager.GetTransactions();

foreach (var r in data)
{
StringContent jsonRequestBody = new StringContent(JsonConvert.SerializeObject(r));
var response = await httpClient.PostAsync("/example/path", jsonRequestBody);

if (!response.IsSuccessStatusCode)
{
DbManager.UpdateTransaction(null, r.TrnID);
}
}
}

{
Console.WriteLine("Stopping!");
timer?.Change(Timeout.Infinite, 0);

}

public void Dispose()
{
timer.Dispose();
}
}
}

• Is DoWork considered Atomic? e.g. when calling StopAsync, does the current execution of DoWork need to finish? – dfhwze Jun 11 '19 at 5:54

If Dowork takes longer than 15 seconds to execute, it will be called a second time. Will this cause issues? One way around that would be to disable the timer while you're doing work, then start it up again when the work is done, or add an appropriately typed and thread safe "busy" member to the class so that you can skip starting any new work if work is currently being done.
• Sorry, I just realized that I didn't mentioned it before. But every 15 seconds DbManager will get new set of data(next 1000 rows). So it doesn't matter if DoWork finished job or not. I think, that multiple threads can work in the same time and that's exactly what I need. – Alexander Ts Jun 11 '19 at 9:29