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
You've definitely implemented it incorrectly. You're returning a Task, but only once all the work has already been done.
It seems to me that you should probably just have a synchronous method:
private static void MyFunction()
{
// Loop in here
}
Then start a task for it like this:
Task task = Task.Run((Action) MyFunction);
You can then await that task if you want - although in the example you've given, there's no point in doing so, as you're not doing anything after the await anyway.
I'd also agree with Reed that using a CancellationToken would be cleaner than a static flag somewhere else.