public void Worker(object token) { if (token is CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { try { using (var connectionWr = connectionsFactory.GetConnectionAsync(CancellationToken.None).Result) { var connection = connectionWr.Connection; DbCommand dbCommand = connection.CreateCommand(); dbCommand.CommandText = "listen test;"; dbCommand.ExecuteNonQuery(); connection.Notification += Connection_Notification; while (!cancellationToken.IsCancellationRequested) { connection.Wait(); } } } catch (Exception ex) { } Thread.Sleep(1000); } } }
private async Task Search(SearchType storedProcedure, string request, DateTime startDt, DateTime endDt, int limit, bool is_channel, bool is_group, CancellationToken token, params long[] chat_ids) { try { string storedProcedureName = null;; switch (storedProcedure) { case SearchType.SearchNamePeriod: storedProcedureName = "search_name_period"; break; case SearchType.SearchInChannel: storedProcedureName = "search_in_channel"; break; case SearchType.SearchPeriod: storedProcedureName = "search_period"; break; default: return; } using (ConnectionWrapper connectionWrapper = await connectionPoolManager.GetConnectionAsync(token)) { NpgsqlCommand searchCommand = connectionWrapper.Connection.CreateCommand(); searchCommand.CommandText = storedProcedureName; searchCommand.CommandType = System.Data.CommandType.StoredProcedure; searchCommand.Parameters.Add(new NpgsqlParameter("request", NpgsqlTypes.NpgsqlDbType.Text)); searchCommand.Parameters.Add(new NpgsqlParameter("lim", NpgsqlTypes.NpgsqlDbType.Integer)); searchCommand.Parameters.Add(new NpgsqlParameter("_is_group", NpgsqlTypes.NpgsqlDbType.Boolean)); searchCommand.Parameters.Add(new NpgsqlParameter("_is_channel", NpgsqlTypes.NpgsqlDbType.Boolean)); searchCommand.Parameters.Add(new NpgsqlParameter("dt1", NpgsqlTypes.NpgsqlDbType.Timestamp)); searchCommand.Parameters.Add(new NpgsqlParameter("dt2", NpgsqlTypes.NpgsqlDbType.Timestamp)); searchCommand.Parameters["request"].Value = request; searchCommand.Parameters["lim"].Value = limit; searchCommand.Parameters["dt1"].Value = startDt; searchCommand.Parameters["dt2"].Value = endDt; searchCommand.Parameters["_is_group"].Value = is_group; searchCommand.Parameters["_is_channel"].Value = is_channel; if (chat_ids != null && chat_ids.Length > 0 && storedProcedure == SearchType.SearchInChannel) { searchCommand.Parameters.Add(new NpgsqlParameter("ids", NpgsqlTypes.NpgsqlDbType.Array | NpgsqlTypes.NpgsqlDbType.Bigint)); searchCommand.Parameters["ids"].Value = chat_ids; } using (NpgsqlDataReader reader = await searchCommand.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { if (!reader.IsDBNull(0) && !reader.IsDBNull(1)) { string name = ""; if (reader.FieldCount >= 3) { if (!reader.IsDBNull(2)) { name = reader.GetString(2); } } searchResultReciever.Recieve(new SearchResult() { Link = reader.GetString(0), Text = reader.GetString(1), Name = name }); } } } //searchResultReciever.IsComplited = true; //return results; } } catch (Exception) { } }