M
michael sorens
I have a WinForm application that, depending on user actions, may spin for a
while doing extensive calculations, showing a progress bar in the meantime.
I would like to be able to launch a second complete copy of the main form so
the user may start a second lengthy calculation if desired.
I have a menu item to "Launch New Workspace" but I am having difficulty
implementing this. I detail my experiments below; any suggestions on the
proper way to do this would be appreciated.
[Environment: WinXP, VS2008, .NET 2.0]
===========
Attempt #1:
===========
This creates a new workspace that works, except that when the first
workspace is tied up doing calculations, this one is locked up as well since
it is in the same thread.
private void LaunchNewWorkspace()
{
new MainForm().Show();
}
===========
Attempt #2:
===========
This attempts to create a new thread.
private void LaunchNewWorkspace()
{
Thread newThread = new Thread(new ThreadStart(NewWorkspaceThreadStart));
newThread.Start();
}
private void NewWorkspaceThreadStart()
{
new MainForm().Show();
}
During initialization of the new new workspace I get this error:
System.Threading.ThreadStateException was unhandled: 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.
The above error occurs on this statement:
this.myComboBox.AutoCompleteMode =
System.Windows.Forms.AutoCompleteMode.SuggestAppend;
===========
Attempt #3:
===========
Variation of previous attempt.
private void LaunchNewWorkspace()
{
Thread newThread = new Thread(new ThreadStart(NewWorkspaceThreadStart));
newThread.Start();
}
[STAThread]
private void NewWorkspaceThreadStart()
{
Application.Run(new MainForm());
}
This resulted in the same error at the same location. Some cursory reading
about the STAThread attribute seemed to indicate it is something that should
not be needed; I am uncertain how to proceed with this.
===========
Attempt #4:
===========
Further research found this pattern, which I am guessing is an older
approach to threading.
private void LaunchNewWorkspace()
{
MainForm workerObject = new MainForm();
Thread newThread = new Thread(workerObject.NewWorkspaceThreadStart);
newThread.Start();
}
private void NewWorkspaceThreadStart()
{
Show();
}
System.InvalidOperationException was unhandled: Cross-thread operation not
valid: Control 'statusStrip' accessed from a thread other than the thread it
was created on.
===========
Attempt #5:
===========
Double-click on my application icon on my desktop
This works, creating a completely independent process, but is not integrated
into the application as a menu choice as I would like.
while doing extensive calculations, showing a progress bar in the meantime.
I would like to be able to launch a second complete copy of the main form so
the user may start a second lengthy calculation if desired.
I have a menu item to "Launch New Workspace" but I am having difficulty
implementing this. I detail my experiments below; any suggestions on the
proper way to do this would be appreciated.
[Environment: WinXP, VS2008, .NET 2.0]
===========
Attempt #1:
===========
This creates a new workspace that works, except that when the first
workspace is tied up doing calculations, this one is locked up as well since
it is in the same thread.
private void LaunchNewWorkspace()
{
new MainForm().Show();
}
===========
Attempt #2:
===========
This attempts to create a new thread.
private void LaunchNewWorkspace()
{
Thread newThread = new Thread(new ThreadStart(NewWorkspaceThreadStart));
newThread.Start();
}
private void NewWorkspaceThreadStart()
{
new MainForm().Show();
}
During initialization of the new new workspace I get this error:
System.Threading.ThreadStateException was unhandled: 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.
The above error occurs on this statement:
this.myComboBox.AutoCompleteMode =
System.Windows.Forms.AutoCompleteMode.SuggestAppend;
===========
Attempt #3:
===========
Variation of previous attempt.
private void LaunchNewWorkspace()
{
Thread newThread = new Thread(new ThreadStart(NewWorkspaceThreadStart));
newThread.Start();
}
[STAThread]
private void NewWorkspaceThreadStart()
{
Application.Run(new MainForm());
}
This resulted in the same error at the same location. Some cursory reading
about the STAThread attribute seemed to indicate it is something that should
not be needed; I am uncertain how to proceed with this.
===========
Attempt #4:
===========
Further research found this pattern, which I am guessing is an older
approach to threading.
private void LaunchNewWorkspace()
{
MainForm workerObject = new MainForm();
Thread newThread = new Thread(workerObject.NewWorkspaceThreadStart);
newThread.Start();
}
private void NewWorkspaceThreadStart()
{
Show();
}
System.InvalidOperationException was unhandled: Cross-thread operation not
valid: Control 'statusStrip' accessed from a thread other than the thread it
was created on.
===========
Attempt #5:
===========
Double-click on my application icon on my desktop
This works, creating a completely independent process, but is not integrated
into the application as a menu choice as I would like.