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>()
		{"R12WD","R12Aer","R12BCA","R12Mag","R12Nov","R12Nov_R","R12EV_Su","R12EV_Giu",
		 "R12AUTO_Giu","R12MAN_Giu","R12Avanti","R12Indietro",
		 "R12RUN","R12Diam_SET","R12I_MRI","R12I_MRS","R12hRS","R12Vel_Rif"};

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

		public frm1()
        {
            InitializeComponent();

			_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;
			list_of_lines.Clear();
			for (int i = 0; i < tables.Count; i++)
			{
				data = String.Format("{0} val={1}i {2}", tables[i], t.Second, unixMilliSeconds);
				list_of_lines.Add(data);
			}
		}
	}
}

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

Can someone help?

Cheers,

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.

Regards

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.

EDIT
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!

Cheers,