Skip to content

Instantly share code, notes, and snippets.

View GeorgeTsiokos's full-sized avatar

George Tsiokos GeorgeTsiokos

View GitHub Profile
@GeorgeTsiokos
GeorgeTsiokos / DataThread.cs
Created January 4, 2019 18:26
UpdateDataGridFromBackgroundThread
public static class DataThread
{
public static Action<DataTable> UpdateDelegate { get; set; }
public static void Execute(TimeSpan updateRate, TimeSpan sleepRate)
{
var key = int.MinValue;
var dataTable = CreateDataTable();
var columns = dataTable.Columns;
var rows = dataTable.Rows;
@GeorgeTsiokos
GeorgeTsiokos / Timeout.cs
Created September 28, 2018 15:37
New CancellationToken w/timeout
public static CancellationToken Timeout(this CancellationToken cancellationToken, TimeSpan timeout)
{
if (!cancellationToken.CanBeCanceled)
return new CancellationTokenSource(timeout).Token;
var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cancellationTokenSource.CancelAfter(timeout);
return cancellationTokenSource.Token;
}
@GeorgeTsiokos
GeorgeTsiokos / FlushConsoleLogger.cs
Created September 28, 2018 13:55
ConsoleLogger may not write all output before the process closes. This code ensures the queue is flushed.
static void Flush(ConsoleLogger consoleLogger)
{
var fieldInfo = typeof(ConsoleLogger).GetField("_queueProcessor", BindingFlags.Instance | BindingFlags.NonPublic);
var consoleLoggerProcessor = (ConsoleLoggerProcessor)fieldInfo.GetValue(consoleLogger);
consoleLoggerProcessor.Dispose();
}
@GeorgeTsiokos
GeorgeTsiokos / ReadOnlySequenceConsumer.cs
Created September 11, 2018 16:02
ReadOnlySequence<byte> TO IReadOnlyList<string> && IReadOnlyList<string> TO ref Span<byte>
internal sealed class ReadOnlySequenceConsumer
{
readonly List<string> _result = new List<string>();
public IReadOnlyList<string> Result => _result;
public void Consume(ReadOnlySequence<byte> message) => _result.Add(GetString(message));
public string GetString(ReadOnlySequence<byte> message) => message.IsSingleSegment ? Encoding.ASCII.GetString(message.First.Span) : string.Create((int)message.Length, message, GetChars);
@GeorgeTsiokos
GeorgeTsiokos / ISerializer.cs
Created August 9, 2018 19:16
Another Serializer
public interface ISerializer
{
bool TryDeserialize<T>([CanBeNull] byte[] value, [CanBeNull] out T result) where T : class;
bool TrySerialize<T>([CanBeNull] T value, [NotNull] out byte[] result, bool compress = false) where T : class;
}
@GeorgeTsiokos
GeorgeTsiokos / FileToConstant.cs
Created July 11, 2018 21:42
Convert a file to a c# byte[] constant
"new byte[] {" + string.Join(", ", File.ReadAllBytes(@"C:\filename")) + "};"
@GeorgeTsiokos
GeorgeTsiokos / DictionaryExtensions.ToXUnitAssertion.cs
Last active July 12, 2018 23:16
ToXUnitAssertion(): verbosely assert a dictionary's contents
public static class DictionaryExtensions
{
public static string ToXUnitAssertion<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> dictionary, string variableName = "dictionary")
{
var isKeyString = typeof(TKey) == typeof(string);
var isValueString = typeof(TValue) == typeof(string);
string GetKey(TKey key) => isKeyString ? $"\"{key}\"" : key.ToString();
string GetValue(TValue value) => isValueString ? $"\"{value}\"" : value.ToString();
var stringBuilder = new StringBuilder();
[PublicAPI]
public static partial class DictionaryExtensions
{
/// <summary>
/// Adds a key/value pair to the <see cref="T:System.Collections.Generic.IDictionary`2" /> by using the specified
/// function, if the key does not already exist.
/// </summary>
/// <param name="dictionary">the dictionary to get or add from</param>
/// <param name="key">The key of the element to add.</param>
/// <param name="valueFactory">The function used to generate a value for the key</param>
@GeorgeTsiokos
GeorgeTsiokos / IgnoreElements`2.cs
Created January 17, 2018 17:55
Rx IgnoreElements and change sequence type
using System;
public static class ObservableExtensions
{
public static IObservable<TResponse> IgnoreElements<TSource, TResponse>(this IObservable<TSource> sequence) => new IgnoreElementsObservable<TSource, TResponse>(sequence);
sealed class IgnoreElementsObservable<TSource, TResult> : IObservable<TResult>
{
readonly IObservable<TSource> _sequence;
@GeorgeTsiokos
GeorgeTsiokos / JsonLogger.cs
Last active January 10, 2018 18:59
Simple high performance c# JsonLogger with auto-flush
public sealed class JsonLogger : IDisposable
{
readonly string _fileName;
readonly JsonSerializer _jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.None,
Converters = new JsonConverter[] {new StringEnumConverter()}
});