1. //线程安全的先进先出队列
    2. private readonly System.Collections.Concurrent.ConcurrentQueue<string> infoQueue = new();
    3. //同步限制工具
    4. readonly SemaphoreSlim _lock = new(1, 1);
    5. void Msg(string info)
    6. {
    7. infoQueue.Enqueue(info);
    8. Task.Run(MsgCore);
    9. }
    10. async void MsgCore()
    11. {
    12. //等待释放钥匙
    13. await _lock.WaitAsync();
    14. {
    15. while (infoQueue.TryDequeue(out var text))
    16. {
    17. await Dispatcher.InvokeAsync(() => state_TextBlock.Text = text);
    18. await Task.Delay(5000);
    19. await Dispatcher.InvokeAsync(() => state_TextBlock.Text = string.Empty);
    20. }
    21. }
    22. //释放钥匙
    23. _lock.Release();
    24. }