This is a migrated thread and some comments may be shown as answers.

RadMessageBoxForm and Ctrl+C in MTA Thread

1 Answer 126 Views
MessageBox
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 18 Mar 2013, 10:43 AM
Hi, 

When I create and show RadMessageBoxForm from System.Threading.ApartmentState.MTA thread I cannot copy text from it by Ctrl+C key pressing.
I got ThreadStateException:
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.

1 Answer, 1 is accepted

Sort by
0
Anton
Telerik team
answered on 21 Mar 2013, 10:44 AM
Hi Thomas,

Thank you for the writing.

The RadMessageBoxForm uses the Clipboard that requires an STA. So the solution in this case is to set the ApartmentState of your thread to be STA. For example:                              
    ThreadStart job2 = new ThreadStart(SecondThreadJob);
    Thread thread2 = new Thread(job2);
    thread2.SetApartmentState(ApartmentState.STA);
    thread2.Start();
 
 
private void SecondThreadJob()
{
    Thread.Sleep(2000);
 
    RadMessageBoxForm form = new RadMessageBoxForm();
 
    form.DialogResult = DialogResult.No;
    form.RightToLeft = RightToLeft.No;
    form.MessageText = "thread2";
 
    form.StartPosition = FormStartPosition.CenterParent;
 
    form.MessageIcon = null;
    form.ButtonsConfiguration = MessageBoxButtons.YesNo;
    form.DefaultButton = MessageBoxDefaultButton.Button1;
 
    form.ShowDialog();
}

There's lots of code in Windows that requires an STA. Notable examples are the Clipboard, Drag & Drop and the shell dialogs (like OpenFileDialog). The UI thread of a WPF or Windows Forms project should always be STA, as does any thread that creates a window.

I hope this helps. Let me know if you have additional questions.

Regards,
Anton
the Telerik team
WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
Tags
MessageBox
Asked by
Thomas
Top achievements
Rank 1
Answers by
Anton
Telerik team
Share this question
or