C# Threading/Async: Running a task in the background while UI is interactable

前端 未结 4 1609

After looking around on both Async/Await and Threading, I\'m still unsure of the right way to apply it to my situation. No matter the variation that I try my UI still hangs

4条回答
  •  自闭症患者
    2020-12-05 07:42

    Instead of trying to use a bool for this, you should consider using the managed cancellation framework built into the framework.

    Basically, you'd build a CancellationTokenSource, and pass a CancellationToken to your method which could be used to handle cancellation.

    Finally, your current method will never get off the UI thread. You'd need to use Task.Run or similar to move the method to the ThreadPool if you don't want to block the UI.

提交回复
热议问题