0

This is my code that works. I wouldn't have to do this if it weren't task based async, but using Monitor.Enter/Exit results in this problem Object synchronization method was called from an unsynchronized block of code. Exception on Mutex.Release()

People have mentioned using AutoResetEvent, and SemaphoreSlim, but I'm not quite sure which pattern fits.

private bool fakelock;

internal async Task<byte[][]> ExchangeCore(byte[][] apdus)
{
    if (apdus == null || apdus.Length == 0)
        return null;
    List<byte[]> resultList = new List<byte[]>();
    var lastAPDU = apdus.Last();

    while (fakelock)
    {
        await Task.Delay(100);
    }

    fakelock = true;

    foreach (var apdu in apdus)
    {
        await WriteAsync(apdu);
        var result = await ReadAsync();
        resultList.Add(result);
    }

    fakelock = false;

    return resultList.ToArray();
}
Christian Findlay
  • 6,770
  • 5
  • 51
  • 103

1 Answers1

1

You could maybe use a SemaphoreSlim which supports async.

private static SemaphoreSlim Semaphore = new SemaphoreSlim(1, 1);

internal async Task<byte[][]> ExchangeCore(byte[][] apdus)
{
    if (apdus == null || apdus.Length == 0)
        return null;

    await Semaphore.WaitAsync();

    try
    {
        List<byte[]> resultList = new List<byte[]>();
        foreach (var apdu in apdus)
        {
            await WriteAsync(apdu);
            var result = await ReadAsync();
            resultList.Add(result);
        }
        return resultList.ToArray();
    }
    finally
    {
        Semaphore.Release();
    }
}
Rogn
  • 969
  • 8
  • 5
  • Dying to test this! I have a gut feeling that SemaphoreSlim in the correct approach, I just didn't have a working example for this pattern, but now I do. Thanks. – Christian Findlay Jul 10 '18 at 01:08
  • Your code made it in to this class: https://github.com/MelbourneDeveloper/ledger-dotnet-api/blob/master/LedgerWallet/Transports/HIDTransportBase.cs which has been submitted as a pull request to the parent repo. – Christian Findlay Jul 10 '18 at 11:41