new Thread(() =>
{
foreach (var fiscal in fiscalList)
new Thread(() => { DisplayResultAsync(fiscal); })
{IsBackground = true, Priority = ThreadPriority.AboveNormal}.Start();
;
}) {IsBackground = true, Priority = ThreadPriority.AboveNormal}.Start();
new Thread(() =>
{
foreach (var fiscal in fiscalList2)
new Thread(() => { DisplayResultAsync(fiscal); })
{IsBackground = true, Priority = ThreadPriority.AboveNormal}.Start();
;
}) {IsBackground = true, Priority = ThreadPriority.AboveNormal}.Start();
static async void DisplayResultAsync(Fiscal fiscal)
{
await fiscal.SaveByteAAsync();
}
public class Fiscal {
private readonly string connectionString;
public Fiscal(NpgsqlConnectionStringBuilder connectionString) {
this.connectionString = connectionString.ConnectionString;
}
public async Task SaveByteAAsync() {
using (var connection = new NpgsqlConnection(connectionString)) {
var connectionTask = connection.OpenAsync();
Task.WaitAll(connectionTask);
if (connectionTask.IsFaulted) throw new Exception("Connection failure", connectionTask.Exception);
Console.WriteLine("Connected! Delaying...");
var delay = Task.Delay(400);
Task.WaitAll(delay);
using (var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)) {
var rawSql =
@"select data.save_bytea_inh(@i_paydate) from generate_series(1, 1, 1);";
using (var command = new NpgsqlCommand(rawSql, connection, transaction)) {
command.Parameters.AddWithValue("i_paydate", NpgsqlDbType.Timestamp, DateTime.Today);
command.Prepare();
await command.ExecuteScalarAsync();
}
await transaction.CommitAsync();
}
Console.WriteLine("Finished working: A");
}
}
}
for (var i = 0; i < 500; i++) {
try {
var task = new Task(async () =>
{
await SaveByteA(connectionString);
});
task.Start();
}
catch (Exception e) {
Console.WriteLine(e);
throw;
}
}
private static async Task SaveByteA(NpgsqlConnectionStringBuilder connectionString) {
using (var connection = new NpgsqlConnection(connectionString.ConnectionString)) {
await connection.OpenAsync();
using (var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)) {
var rawSql =
@"select data.save_bytea_inh(@i_paydate) from generate_series(1, 1, 1);";
using (var command = new NpgsqlCommand(rawSql, connection, transaction))
{
command.Parameters.AddWithValue("i_paydate", NpgsqlDbType.Timestamp, RandomDay());
command.Prepare();
await command.ExecuteScalarAsync();
};
await transaction.CommitAsync();
}
Console.WriteLine("Finished working: A");
}
}