SMahdiS
SMahdiS

Reputation: 940

Adding controls in background thread?

I am trying to create some controls after the user logged in.

I have a TabControl with 3 tabs: the first one to login, the second one is a place that needs to show lots of tiles (MahApps.Metro Tile). Each tile represent a hotel room.

And the third tab displays an indeterminate progress bar. When the user logs in, this tab is displayed.

How I did this:

    //User is logged in, so display the progress bar and add controls
    tabControl.SelectedIndex = 2; // display the progress bar tab

    private void populateHotel(Hotel hotel)
    {
        List<Room> rooms = hotel.rooms;
        Tile tile;
        tile_wrapper.Children.Clear();
        foreach (Room room in rooms)
        {
            sql.insertHotelRoom(room);
            tile = new Tile();
            tile.Title = room.room_num;
            tile.Content = room.name_fa;              
            tile.FontFamily = titleRooms.FontFamily; // titleRooms is a textbox
            tile.TitleFontSize = item1.TitleFontSize; // item1 is an already created tile
            tile.FontSize = item1.FontSize;
            tile_wrapper.Children.Add(tile);

        }
        // Now display the rooms tab
        tabControl.SelectedIndex = 1;
    }

The problem is, this freezes the UI thread and everything freezes, including the progress bar until it creates all the tiles.

How can I do the add controls so the progress bar still responds?

Upvotes: 0

Views: 88

Answers (2)

Sefe
Sefe

Reputation: 14017

What you're doing is to mix the business code that inserts the Hotel with the UI code that displays it. It's not a good idea to mix business code and UI code. Along with the maintainability issues this creates, it is contributing to your problem, because the UI has to wait for your insertHotelRoom operations and can't react to user input.

The first thing you should do is to separate your concerns:

private List<Room> insertHotelRooms(Hotel hotel)
{
    List<Room> rooms = hotel.rooms;
    foreach (Room room in rooms)
    {
        sql.insertHotelRoom(room);
        rooms.Add(room);
    }
    return rooms;
}


private void populateHotelTab(List<Room> rooms)
{
    Tile tile;
    tile_wrapper.Children.Clear();
    foreach (Room room in rooms)
    {
        tile = new Tile();
        tile.Title = room.room_num;
        tile.Content = room.name_fa;              
        tile.FontFamily = titleRooms.FontFamily; // titleRooms is a textbox
        tile.TitleFontSize = item1.TitleFontSize; // item1 is an already created tile
        tile.FontSize = item1.FontSize;
        tile_wrapper.Children.Add(tile);

    }
    // Now display the rooms tab
    tabControl.SelectedIndex = 1;
}

You can now run the operation in a task and dispatch the UI operation back to the UI thread:

Task insertRoomsTask = Task.Run( () => {
    List<Room> rooms = insertRooms(hotel);
    tile_wrapper.Dispatcher.Invoke(() => populateHotelTab(rooms));
});

Upvotes: 1

Troy Mac1ure
Troy Mac1ure

Reputation: 647

//User is logged in, so display the progress bar and add controls
tabControl.SelectedIndex = 2; // display the progress bar tab

private void populateHotel(Hotel hotel)
{
    List<Room> rooms = hotel.rooms;
    Tile tile;
    tile_wrapper.Children.Clear();

    // Use a count variable to keep track of how many rooms have been processed
    int count = 0;

    foreach (Room room in rooms)
    {
        // Update the value of the progress bar
        progressbar1.Value = count++ * 100 / rooms.Count;
        // Cause the progress bar to refresh
        progressbar1.Refresh();

        sql.insertHotelRoom(room);
        tile = new Tile();
        tile.Title = room.room_num;
        tile.Content = room.name_fa;              
        tile.FontFamily = titleRooms.FontFamily; // titleRooms is a textbox
        tile.TitleFontSize = item1.TitleFontSize; // item1 is an already created tile
        tile.FontSize = item1.FontSize;
        tile_wrapper.Children.Add(tile);

    }
    // Now display the rooms tab
    tabControl.SelectedIndex = 1;
}

Upvotes: 0

Related Questions