Question : How to add to TabControl from other thread?

In a WPF, .NET 3.5, C# application, I'm creating TabControls and TabItems on a seperate thread from the UI.  This will then be added to a TabControl already on the UI thread.  Below is the code I use to do assignments on items contained on the UI thread, which work fine.   I can assign to properties but if I try to add a TabItem from the 2nd thread to a TabControl on the UI thread, I get this exception:

Additional information: Exception has been thrown by the target of an invocation.

Any ideas why this happens?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
private delegate void updateProgress();
 
//start thread
Thread thread = new Thread(new ThreadStart(doSomething));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
 
void doSomething(){
updateProgress add = new updateProgress(addToMainTab);
//TabControl on UI thread
tabMain.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, add);
}
 
private void addToMainTab()
{
  //this will succeed
  ((TabItem)tabMain.Items[0]).Header = "just changed it";
  //this will fail
  tabMain.Items.Add(item);
}

Answer : How to add to TabControl from other thread?

Hi brettr;

Now I understand that you are building the TabItem control in the thread, was not stated in the original post. The fact is that you can not create the control in the thread and add it to the TabControl in that manner. You must create the control in the main thread. The sample code below is one way of doing it. Doing the time consuming work in the thread and creating the TabItem in the  main thread.

Fernando
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
 
 
namespace WpfApplication1
{
    /// 
    /// Interaction logic for Window1.xaml
    /// 
    public partial class Window1 : Window
    {
 
        private delegate void updateProgress();
        TabItem item = null;
        // Used to create a TabItem control in Background thread
        TabItemHelper tabItemHelper = null;
 
        public Window1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //start thread
            Thread thread = new Thread(new ThreadStart(doSomething));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }
 
        void doSomething()
        {
            updateProgress add = new updateProgress(addToMainTab);
            // Create the TabItemHelper class 
            tabItemHelper = new TabItemHelper();
            tabItemHelper.Header = "New Tab Item";
            tabItemHelper.Name = "NTI1";
            tabItemHelper.HorizontalAlignment = HorizontalAlignment.Right;
            tabItemHelper.FontFamily = new FontFamily("Tahome");
            tabItemHelper.FontSize = 24.0;
            
            //TabControl on UI thread
            tabMain.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, add);
        }
 
        private void addToMainTab()
        {
            //this will succeed
            ((TabItem)tabMain.Items[0]).Header = "just changed it";
            // Use the tabItemHelper class to populate the new TabItem control
            item = new TabItem() { Header = tabItemHelper.Header, Name = tabItemHelper.Name, 
                HorizontalAlignment = tabItemHelper.HorizontalAlignment, FontFamily = tabItemHelper.FontFamily,
                FontSize = tabItemHelper.FontSize };
            //this will succeed
            tabMain.Items.Add(item);            
        }
 
    }
}
 
class TabItemHelper
{
    private String header = String.Empty;
    private String name = String.Empty;
    private HorizontalAlignment horizontalAlignment;
    private FontFamily fontFamily;
    private Double fontSize = 0; 
 
    public String Header
    {
        get { return header; }
        set { header = value; }
    }
 
    public String Name
    {
        get { return name; }
        set { name = value; }
    }
 
    public HorizontalAlignment HorizontalAlignment
    {
        get { return horizontalAlignment; }
        set { horizontalAlignment = value; }
    }
 
    public FontFamily FontFamily
    {
        get { return fontFamily; }
        set { fontFamily = value; }
    }
 
    public Double FontSize
    {
        get { return fontSize; }
        set { fontSize = value; }
    }
 
}
Random Solutions  
 
programming4us programming4us