I was wondering if there was any easy way to have the save file dialog run in the same thread as the main window of my program?
private void saveAs_click(object sender, EventArgs e)
{
System.Windows.Forms.SaveFileDialog saveDiag = new System.Windows.Forms.SaveFileDialog();
saveDiag.ShowDialog();
}
I am getting the following error:
An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll
Additional information: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.
I created an about dialog which I called by:
private static void aboutThreadProc()
{
Application.Run(new aboutDialog());
}
but I want to avoid doing this again for the save dialog, unless anyone knows of an easy way to pass the information the user enters into the dialog back to the main program??
sorry, I'm getting my processes and threads a little mixed up here, which is really confusing me considering this is the first GUI application I have written, so I don't exactly know how to get everything to communicate yet.
edit:
my main windows is started with this:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
resultsStore workingStore = new resultsStore();
System.Threading.Thread mainThread = new System.Threading.Thread(new System.Threading.ThreadStart(mainThreadProc));
mainThread.Start();
}
and
private static void mainThreadProc()
{
Application.Run(new main());
}
Open to any suggestions
Solution:
What is the purpose of spawning your own "main thread"? There is already a main thread which pumps window messages for you. That's your issue. Remove that and just have Application.Run(new main()); without the thread. – Simon Whitehead 10 mins ago