C# library - WriteRecords is not writing records

Hi All,

I’m trying to write a list of points to an InfluxDB through a C# windows form application ( .NET 6 )
InfluxDB 2.3
InfluxDB Client library 4.3.0
The code is pretty simple so I really don’t know what is going on.

using System.Data;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;

namespace Test
    public partial class frm1 : Form
		const string token = "Token from influxDB WebPage";
		const string bucket = "Test15";
		const string org = "Company";

        InfluxDBClient _client;
		WriteApi _writeApi;

		List<string> list_of_lines = new List<string>();
		List<string> tables = new List<String>()

		long unixMilliSeconds;
		string data = "";
		DateTime t;

		public frm1()

			_client = InfluxDBClientFactory.Create("http://localhost:8086", token);
			//var health = _client.PingAsync();
			//MessageBox.Show(String.Format("Health: {0}", health.Status));
			_writeApi = _client.GetWriteApi();


        private void button1_Click(object sender, EventArgs e)
			t = DateTime.Now;
			unixMilliSeconds = (t.ToUniversalTime().Ticks - new DateTime(1970, 1, 1).Ticks) / 10000;
			for (int i = 0; i < tables.Count; i++)
				data = String.Format("{0} val={1}i {2}", tables[i], t.Second, unixMilliSeconds);

It just doesn’t insert the points as it should.
The same code on a Windows Console Application (C#) is working.

Can someone help?


Hi @Cristian_Bertulli,

thanks for using our client.

The _client.GetWriteApi() uses background thread for ingesting data. Do you dispose the client at the end of your script?

Maybe the _client.GetWriteApiAsync() will be better for your use case because the WriteApiAsync directly ingest data into InfluxDB.


Hi @bednar,
Thanks for your reply.

the copy-paste somehow forgot the last line of code, which is:

_writeApi.WriteRecords(list_of_lines, WritePrecision.Ms, bucket, org);

I tried with the dispose _writeApi.Dispose(); and without. Same results.
I’ll try what you suggest about the writeAsync and let you know.

I just tried with _client.GetWriteApiAsync(); and _writeApiAsync.WriteRecordsAsync(list_of_lines, WritePrecision.Ms, bucket, org);
and I was finally able to write data!

Thanks a lot for the help!
Have a nice day!