0

So i have a small issue... When going from one window(start window) and starting up second window(Main window), the second window is very heavy and need some loading time of around 10 secs, which means that the start window would freeze for 10 seconds then start Second window and close itself.

So Running This function takes 10 sec: BoardTable table = new BoardTable() And i would like to have some live loading symbol/graphics while it's loading for 10 seconds and not freeze up whole UI.

*I have tried loading the second window on another thread result => Static thread error...

*Loaded With Static Thread(Got this idea from another post here at stackoverflow)... Either UI would freeze or second table would not load at all.

So im a little bit confused of how to implement a Loading Symbol of some sort when the UI just keeps Freezing :(

CoffeDev
  • 258
  • 2
  • 12
  • In theory, it should work say if you did a `Task.Run(()=>{ var window = new BoardTable(); window.Show();});`. However, this will create a **second** UI thread and the two windows shall never meet again. You'll need to carefully partition the code to avoid cross-threading bugs which will no doubt happen. And you may encounter a thread-apartment related exception in that `Task.Run()` call itself, may need to start a brand new `Thread` instead. – Tanveer Badar Nov 29 '19 at 09:12
  • Are you familiar with async and Tasks? – geometrikal Nov 29 '19 at 09:13
  • It would be much better if you _can_ figure out why the second window takes so long to load and optimize that. Other things are work-arounds at best and do not improve the user experience itself. 10s is still 10s even when executed on a different thread. – Tanveer Badar Nov 29 '19 at 09:14
  • See also [UI Freezes when calling .Show on new Window](https://stackoverflow.com/questions/58590614/ui-freezes-when-calling-show-on-new-window/58591795#58591795) – Rekshino Nov 29 '19 at 09:21

1 Answers1

1

I actually experimented a working method, Sharing it here incase someone else has the same problem in future:

StartWindow:

        Thread t = new Thread(new ParameterizedThreadStart(LoadSecondWindow));
    t.SetApartmentState(ApartmentState.STA);
    t.Start(gameMode);

on a new thread:

        private void LoadSecondWindow(object o)
    {
        GameMode gameMode = (GameMode)o;
        Table blackjackTable = new Table(gameMode);
        blackjackTable.Show();
        Dispatcher.Run();
    }

Dispatcher.Run() Was actually what solved it all... Now we can render some UI loading symbols for whatever amount of time we need :D

CoffeDev
  • 258
  • 2
  • 12