Entwickler-Ecke
Basistechnologien - STAThread - Probleme mit Threading
marix - Mo 08.11.10 16:15
Titel: STAThread - Probleme mit Threading
Hallo Forummitglieder,
Ich habe eine kleine eigene Messagebox erstellt, die als Alternative in einer Anwendung Warnmeldungen erzeugt.
Diese sollte als STAThread aufgerufen werden.
Es werden dabei 2 Strings übergeben.
Ich bin schon seit tagen am googeln und habe schon sämtliche Verweise auf ähnliche Probleme strikt durchgearbeitet aber ich kann den Fehler nicht beheben.
Hier der relevante Teil des Codes:
C#-Quelltext
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:
| namespace Mainproject.WPFMessageBoxControls { internal delegate void Invoker();
public partial class WPFMessageBox : Window { #region Constants private const int YES_ID = 356; private const int NO_ID = 357; private const int OK_ID = 65; private const int CANCEL_ID = 64; public string strText; public string strCaption; public string strTextNew; public string strCaptionNew; public string[] strTC; public object data;
#endregion
#region variables #endregion
[ThreadStatic] public static WPFMessageBox _msgBox; public string strYes, strCancel, strOk, StrNo;
public WPFMessageBox(string strText, string strCaption) {
InitializeComponent(); this.tblMessage.Text = strText; this.lblMsgWndHeader.Content = strCaption; this.InitializeMultiLangText(); this.OnLanguageChanged();
}
[STAThread] public static void ThreadProc(object data) { object[] ParamArray = (object[])data; string strText = (string)data[1]; string strCaption = (string)data[2]; WPFMessageBox WPFMBox = new WPFMessageBox(strText, strCaption); _msgBox.ShowDialog(); }
[STAThread] public static bool? ShowMessage(string strText, string strCaption, WFPMessageBoxButtons msgBxBtn, MessageBoxImage image)
{ Thread newThreadProc = new Thread (new ParameterizedThreadStart(WPFMessageBox.ThreadProc));
string strTextNew = strText; string strCaptionNew = strCaption; object[] data = new object[] { strTextNew, strCaptionNew }; newThreadProc.SetApartmentState(ApartmentState.STA); newThreadProc.Name = "STAThread"; newThreadProc.Start(_msgBox.data);
if (System.Windows.Application.Current.Properties["MainWindow"] is Window) { Window wndw = System.Windows.Application.Current.Properties["MainWindow"] as Window; _msgBox.Owner = System.Windows.Application.Current.Properties["MainWindow"] as Window; }
UpdateUI(msgBxBtn); _msgBox.Close(); return _msgBox.DialogResult; } |
Die Fehlermeldung lautet:
Cannot apply indexing with [] to an expression of type 'object'
Kann mir jemand etwas Licht ins Dunkel bringen???
Herzlichen Dank
Gruss Markus ---marix---
bakachan - Mo 08.11.10 16:24
Du versuchst ja auch per Index auf ein einzelnes Object(data) zuzugreifen anstatt auf das object[] (ParamArray ) das du erstellt bzw gecastet hast.
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8:
| public static void ThreadProc(object data) { object[] ParamArray = (object[])data; string strText = (string)data[1]; string strCaption = (string)data[2]; WPFMessageBox WPFMBox = new WPFMessageBox(strText, strCaption); _msgBox.ShowDialog(); } |
marix - Mo 08.11.10 17:19
Hallo bakachan,
genau, ParamArray statt data... :idea:
vielen Dank, manchmal sieht man den Wald vor Bäumen nicht.
Leider habe ich jetzt bei:
newThreadProc.Start(_msgBox.data);
eine SystemNullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
evtl. noch eine Idee???
Viele Grüsse Markus
bakachan - Mo 08.11.10 17:25
ähnliches Prinzip wie das letzte Problem:
C#-Quelltext
1: 2:
| WPFMessageBox WPFMBox = new WPFMessageBox(strText, strCaption); _msgBox.ShowDialog(); |
WPFMBox erstellt aber _msgBox(== null?) aufgerufen.
marix - Mo 08.11.10 22:12
Danke vielmals, ich komme aus der C++ Welt und habe einige Zeit nicht mehr programmiert, danke für die Hilfe.
Eine generelle Frage noch, wie kann ich solch einen Dialog generell von meiner MainGUI sepparieren, die selbstkreeirte MessageBox sollte generell unabhängig vom MainThread aufgerufen werden können, bin ich denn mit meinem Entwurf auf dem richtigen Weg?
Ich kann auch jeden Aufruf der MessageBox dispatchen, um Exceptions zu vermeiden, aber da es unheimlich viele Aufrufe sind, wäre eine zentrale Lösung viel besser.
Sorry für die banalen Fragen, wenn das zu sehr in die Grundlagen geht, schliesse ich diesen Thread als abgeschlossen ab.
Learning by Doing ist immer der beste Weg, aber manchmal brauch man einen Mentor der einem den Weg zeigt.
Viele Grüsse Markus
Entwickler-Ecke.de based on phpBB
Copyright 2002 - 2011 by Tino Teuber, Copyright 2011 - 2025 by Christian Stelzmann Alle Rechte vorbehalten.
Alle Beiträge stammen von dritten Personen und dürfen geltendes Recht nicht verletzen.
Entwickler-Ecke und die zugehörigen Webseiten distanzieren sich ausdrücklich von Fremdinhalten jeglicher Art!