You can find a lot of similar questions on SO, but no one (as I'm see) covers situation, when your logic must return something.
In this code example I have simple CustomMessageBox (it's a window), which must return something, entered by user.
public class CustomMessageBox
{
private string Value
{
get
{
return txt_box.Text;
}
}
private CustomMessageBox ()
{
InitializeComponent();
}
public static string Show(string caption = "Enter data")
{
CustomMessageBox cmb = new CustomMessageBox ();
cmb.txt_block.Text = caption;
cmb.ShowDialog();
return cmb.Value;
}
}
So when Show method called by BackgroundWorker, exception thrown at first line, when constructor tries to execute. Exception message is
An exception of type 'System.InvalidOperationException' occurred in
PresentationCore.dll but was not handled in user code
Additional information: The calling thread must be STA,
because many UI components require this.
Nothing new, but I can't find solutions for this problem, and I can't make thread to be STA. Show method signature must be clear like this — take string and return string.
How usually things like this must be resolved?