Skip to content

Instantly share code, notes, and snippets.

@wi7a1ian
wi7a1ian / client.cs
Created February 23, 2024 16:47
Using Windows named pipes in #csharp
using System.IO.Pipes;
var httpHandler = new SocketsHttpHandler
{
ConnectCallback = async (ctx, ct) =>
{
var pipeClientStream = new NamedPipeClientStream(
serverName: ".",
pipeName: "my-test-pipe",
PipeDirection.InOut,
@wi7a1ian
wi7a1ian / Usage.cs
Created September 7, 2022 10:42
Useful classes for data-driven tests in #csharp
[Theory]
[XUnitJsonFileData("TestData/Some.json", typeof(SomeTestData))]
public void ShouldDoSth(SomeTestData data) {}
[TestMethod]
[DeploymentItem("TestData/Some.xml")]
[MsTestXmlFileData("Some.xml", "DataSet", "In1", "In2", "In3", "ExpectedOutput")]
public void ShouldDoSth(string in1, string in2, string in3, string output) {}
@wi7a1ian
wi7a1ian / BuilderBase.cs
Created September 7, 2022 10:34
Builder base class in #csharp
using System;
using System.Threading.Tasks;
namespace FooBar
{
public interface IBuilder { }
public interface IBuilder<TBuildResult, TBuilder> : IBuilder
where TBuildResult : class, new()
where TBuilder : class, IBuilder
@wi7a1ian
wi7a1ian / async-enumerable.cs
Created October 7, 2020 09:31
Explanation of `EnumeratorCancellation` in async enumerable #async #csharp
public class Program
{
// https://docs.microsoft.com/en-us/archive/msdn-magazine/2019/november/csharp-iterating-with-async-enumerables-in-csharp-8
public static async Task Main(string[] args)
{
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(300));
/*
* Passing the token directly to the method is easier, but it doesn’t work when you’re handed an arbitrary IAsyncEnumerable<T>
* from some other source but still want to be able to request cancellation of everything that composes it.
* In corner-cases, it can also be advantageous to pass the token to GetAsyncEnumerator, as doing so avoids “burning in” the token
@wi7a1ian
wi7a1ian / CustomException.cs
Created August 24, 2020 10:38
Serializable custom exceptions #csharp
[Serializable]
public class CustomException : Exception
{
public CustomException()
{
}
public CustomException(string message) : base(message)
{
}
public CustomException(string message, Exception innerException) : base(message, innerException)
@wi7a1ian
wi7a1ian / modern-cpp-techniques-00.cpp
Last active November 22, 2019 11:55
Modern C++ techniques based on "Modern Techniques for Keeping Your Code DRY" by Bjorn Fahller #cpp #notes
enum state_type {IDLE, CONNECTING, CONNECTED, DISCONNECTING, DISCONNECTED};
// the problem
assert(state == IDLE || state == DISCONNECTING || state == DISCONNECTED);
@wi7a1ian
wi7a1ian / code-1-before.cs
Last active September 19, 2023 12:38
Async/await internals in C# #csharp
class Program
{
static void Main(string[] args)
{
MainAsync().Wait();
}
static async Task MainAsync()
{
CancellationTokenSource cts = new CancellationTokenSource();
@wi7a1ian
wi7a1ian / using-expected.cpp
Last active October 31, 2019 07:12
Using std::expected in C++ #cpp #functional
auto parse_int(std::string_view str)
-> std::expected<int, std::domain_error>
{
int i;
std::stringstream ss;
ss << str;
ss >> i;
if(ss.fail())
return std::make_unexpected( std::domain_error("value is not an integer") );
return i;
@wi7a1ian
wi7a1ian / dndd2019-notes.md
Last active February 11, 2022 09:37
Notes from .NET Dev Days 2019 Warsaw conference #notes

.NET Dev Days Workshop

repo

Talk #1 Crash & Exceptions

  • Watch -> $(exception).InnerException ...
  • First time ext - break on any CLR ext
  • DebugerDisplay vs ToString()
  • Just my Code -> uncheck to troubleshoot timer tick
  • Threads window
  • Debuging > Smbols > MS Sym Serv
@wi7a1ian
wi7a1ian / DataFlowExamples.cs
Created October 7, 2019 10:09
Usage of three basic DataFlow types in C# #csharp
static void Main(string[] args)
{
var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(10));
TestProducerConsumerViaBufferBlock(cts.Token);
TestProducerConsumerViaActionBlock(cts.Token);
TestParallelTransformViaTransformBlock(cts.Token);
}