RadMessageBoxForm and Ctrl+C in MTA Thread

2 posts, 0 answers
  1. Thomas
    Thomas avatar
    22 posts
    Member since:
    Oct 2012

    Posted 18 Mar 2013 Link to this post

    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.
  2. Anton
    Admin
    Anton avatar
    167 posts

    Posted 21 Mar 2013 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top