Autor Beitrag
DareDevil
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70

Windows7
C# (VS 2010)
BeitragVerfasst: Mi 21.07.10 10:13 
Ich habe das Problem, dass ich aus einem Thread kein GUI Elements in mein GUI bekomme.

Hier mal mein Code.

Delegate Aufruf:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
public void addTreeViewItem(TreeViewItem tvi)
{
  if(this.myTreeView.Dispather.CheckAccess)
  {
    this.myTreeView.Items.Add(tvi);
  }
  else
  {
    AddTreeViewItem d = new AddTreeViewItem(addTreeViewItem);
    this.Dispather.Invoke(d, tvi);
  }
}


Funktion im Thread:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
public void createGuiElement()
{
  TreeViewItem tvi = new TreeViewItem();
  tvi.Header = "Text";
  MainWindow.addTreeViewItem(tvi);
}


Jetzt bekomme ich die Fehlermeldung das auf "tvi" im GUI-Thread nicht zugegriffen werden kann, da es zu einem anderen Thread gehört.

Vielleicht hat ja einer ne Lösung wie ich mein Problem behoben bekomme.

Gruß
Pascal
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 21.07.10 10:17 
Ich denke, Du musst das TreeViewItem selber schon im GUI-Thread erstellen.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
DareDevil Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70

Windows7
C# (VS 2010)
BeitragVerfasst: Mi 21.07.10 10:23 
Hm das ich echt ein doofer unterschied zwischen WPF und WinForms.

Naja muss ich halt mal schauen ob ich es irgendwie in diese Richtung umgebaut bekomme.

In dem TreeViewItem hängen ja leider noch andere Items dran.
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mi 21.07.10 18:57 
Darin sollte sich Winforms nicht groß unterscheiden, die Einschränkung gilt Windows-weit.

_________________
>λ=
DareDevil Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70

Windows7
C# (VS 2010)
BeitragVerfasst: Do 22.07.10 08:12 
Kha leider unterscheidet sich WinForms und WPF darin.

Kannst es ja ganz einfach testen.

In einem Thread einen TreeNode/TreeViewItem anlegen und per Invoke ans TreeView anhängen.

Bei WinForms geht es ohne Probleme. Nur bei WPF bekommen man einen Thread Zugriffsfehler.