I can't figure this out... I have followed several different instructios I have found everywhere, but I can't find out how to not freeze the UI thread whilst performing a few tasks.
I run this when the window loads. In root of window in MainWindow.xaml I have:
<i:Interaction.Triggers>
<i:EventTrigger EventName="ContentRendered">
<i:InvokeCommandAction Command="{Binding WindowLoaded}" />
</i:EventTrigger>
</i:Interaction.Triggers>
That triggers the following:
private void WindowLoadedEx(object p)
{
DBMan();
LoadValues();
//Boot = false;
//MainMenuVisibility = true;
}
DBMan() is negligable, the problem isn't here - it is in LoadValues(). I have chopped and changed this a lot trying out various different things, nothing working.
private void LoadValues()
{
//ThreadStart AddHeroesRef = new ThreadStart(HeroesDBAddHeroes);
//Thread AddHeroesThread = new Thread(AddHeroesRef);
//AddHeroesThread.Start();
////HeroesDBAddHeroes();
//ThreadStart AddCommentsRef = new ThreadStart(HeroesDBAddComments);
//Thread AddCommentsThread = new Thread(AddCommentsRef);
//AddCommentsThread.Start();
////HeroesDBAddComments();
ThreadStart LoadValuesRef = new ThreadStart(LoadValuesThreaded);
Thread LoadValuesThread = new Thread(LoadValuesRef);
LoadValuesThread.Start();
//HeroesDBAssignComments();
//SetDBIDs();
}
private async void LoadValuesThreaded()
{
ThreadStart AddHeroesRef = new ThreadStart(HeroesDBAddHeroes);
Thread AddHeroesThread = new Thread(AddHeroesRef);
AddHeroesThread.Start();
//HeroesDBAddHeroes();
ThreadStart AddCommentsRef = new ThreadStart(HeroesDBAddComments);
Thread AddCommentsThread = new Thread(AddCommentsRef);
AddCommentsThread.Start();
//HeroesDBAddComments();
while (AddHeroesThread.IsAlive || AddCommentsThread.IsAlive)
{
Thread.Sleep(1);
}
HeroesDBAssignComments();
SetDBIDs();
Boot = false;
MainMenuVisibility = true;
}
(As you can see, ive tried changing different things and cant get anything to work).
Basically, both HeroesDBAddHeroes() and HeroesDBAddComments() need to be run together, they have no conflicts with each other etc. I will in future have other methods to run here also, at the same time in seperate threads.
However, I need to wait for these to all be complete before continuing and allowing HeroesDBAssignComments() to run. If everything above isnt complete already, it wont work. Then, I need to change the booleans Boot and MainMenuVisibility once HeroesDBAssignComments is complete.