mkjeff / secs4net

SECS-II/HSMS-SS/GEM implementation on .NET
https://mkjeff.github.io/secs4net/
MIT License
443 stars 195 forks source link

Cannot cast type "PooledAwait.ValueTaskCompletionSource1[System.Object]" to type "System.Threading.Tasks.TaskCompletionSource1[System.Object]" #57

Closed tossorrow closed 3 years ago

tossorrow commented 3 years ago

I created 2 buttons in winform app.

SecsGem? _secsGem;
HsmsConnection? _connector;
CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();

private async void StartButton_Click(object sender, EventArgs e)
{
    _secsGem?.Dispose();
    if (_connector is not null)
    {
        await _connector.DisposeAsync();
    }
    var options = Options.Create(new SecsGemOptions
    {
        IsActive = false,
        IpAddress = "127.0.0.1",
        Port = 5000,
        SocketReceiveBufferSize = 65535,
        DeviceId = (ushort)0,
    });
    var logger = new SecsLogger();
    _connector = new HsmsConnection(options, logger);
    _secsGem = new SecsGem(options, _connector, logger);
    _ = _connector.StartAsync(_cancellationTokenSource.Token);
    await foreach (var primaryMessage in _secsGem.GetPrimaryMessageAsync(_cancellationTokenSource.Token))
    {
        recvBuffer.Add(primaryMessage);
    }
}

private async void EndButton_Click(object sender, EventArgs e)
{
    if (!_cancellationTokenSource.IsCancellationRequested)
    {
        _cancellationTokenSource.Cancel();
        _cancellationTokenSource.Dispose();
    }
    if (_connector is not null)
    {
        await _connector.DisposeAsync();
    }
    _secsGem?.Dispose();
    _cancellationTokenSource = new CancellationTokenSource();
    _secsGem = null;
    recvBuffer.Clear();
}

I click start button and then click end button, exception occured in _cancellationTokenSource.Cancel(). Cannot cast an object of type "PooledAwait.ValueTaskCompletionSource1[System.Object]" to type "System.Threading.Tasks.TaskCompletionSource1[System.Object]".

mkjeff commented 3 years ago

Could you provide a complete source repository? That would be helpful for the people wanna help.

tossorrow commented 3 years ago

Thanks for reply, here‘s the complete source repository https://gitee.com/tossorrow/forissue_secs.git

mkjeff commented 3 years ago

I have addressed the issue in this commit If the problem solves, I'd publish a new version later.

tossorrow commented 3 years ago

problem solved, thx