//线程安全的先进先出队列
private readonly System.Collections.Concurrent.ConcurrentQueue<string> infoQueue = new();
//同步限制工具
readonly SemaphoreSlim _lock = new(1, 1);
void Msg(string info)
{
infoQueue.Enqueue(info);
Task.Run(MsgCore);
}
async void MsgCore()
{
//等待释放钥匙
await _lock.WaitAsync();
{
while (infoQueue.TryDequeue(out var text))
{
await Dispatcher.InvokeAsync(() => state_TextBlock.Text = text);
await Task.Delay(5000);
await Dispatcher.InvokeAsync(() => state_TextBlock.Text = string.Empty);
}
}
//释放钥匙
_lock.Release();
}