I have an app which can load XML files and then parse it into some different classes. It has it's own window with button like "Load File"
There are some schematic code what it looks like
///In main window
public void Button_Click ()
{
//(VM)DataContext.AddNewFile();
}
///In VM
public void AddNewFile()
{
//Some code with file dialogs and messages with several methods, one of which leads to AddMyType()
}
private void AddMyType(string file)
{
//Creates class of myType
}
///In Type class constructor
internal MyType(string file)
{
//Launches XML parser class to parse data from XML and create from this all MyType
data = ParseFile(file).Result;
}
///In XML parser class:
internal async Task<MyTypeData>ParseFile(file)
{
//Main method which launches background tasks and shows messages with progress bar
//Should update UI
_progress = new Progress<int>(number =>
{
_message.UpdateProgress(number);
});
someData = await Task.Run(()=> BackgroundWorkTask(rawData, _progress));
_message.Show(_maximumProgressValue); //Shows custom message with progress bar
someOtherData = await Task.Run(()=> BackgroundWorkTask(otherRawData, _progress));
}
private <T> Task BackgroundWorkTask(rawData)
{
//Some big work going here and gives some result
}
The problem is that UI freezes.
I tried many variants:
Task.Wait(), await BackgroundWorkTask and tested it with
private <T> Task BackgroundWorkTask()
{
//Freezes
while(true){}
Task.Delay(x)
Task.Delay(x).Wait()
Thread.Sleep(x)
and await Task.Delay(x) (if private async Task)
}
I guess it's because all main work BEFORE is going in sync mode in main thread. But i really have lots of different operations with data in main thread before start ParseFile() method.
As I understand async method with await Task.Run(()=>); is used to do some work on other thread and shouldn't block UI.
I thought that using async method where I will update UI with await different tasks can help but....
If there a way to solve this problem?