Hi guys, so im hoping you can help me with this...
The method "window" below is attempting to launch a new thread, which will open up the window "MyDialog"
the window contains a text box, and a button, so i make the method wait while the mydialog window thread is open...
the user then enters a pin into the textbox, presses the button....
the button then adds the "email" string, and the "responcetext" to a dictonary, before closing the window, which should then close the thread, and the code continue.... great!
this seems to work perfectly when i have one thread open, running this....
private void window(string input) { Thread thread = new Thread(() => { MyDialog w = new MyDialog(input); w.Show(); w.Closed += (sender2, e2) => w.Dispatcher.InvokeShutdown(); System.Windows.Threading.Dispatcher.Run(); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); while (thread.IsAlive == true) { Thread.Sleep(1000); } }
partial class MyDialog : Window { string email; public static MyDialog screenMain = null; public MyDialog(string emailinput) { email = emailinput; InitializeComponent(); screenMain = this; name.Content = "Please enter the pin relating to " + email; } public string ResponseText { get { return ResponseTextBox.Text; } set { ResponseTextBox.Text = value; } } private void OKButton_Click(object sender, System.Windows.RoutedEventArgs e) {
LoginRequest.setMyVariable2(email.ToString(), ResponseText.ToString()); Close(); } } }
as soon as i have two threads open, both running this same code at once from seperate threads i get this error happening:
An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
Additional information: The Application object is being shut down.
It happens on the Initializecomponent(); line....
and if i continue i get object null errors on the name.Content line
i though if i opened the mydialog window as a new thread again, i would avoid these errors? Im new to coding so appologies if im being stupid here....
if anyone can help me out id be very very very grateful!