Copy to clipboard (different thread)

2 posts, 0 answers
  1. Dominik
    Dominik avatar
    59 posts
    Member since:
    Dec 2012

    Posted 28 Oct 2015 Link to this post

    Hello,

    I open the window containing RadGridView as follows:

    new System.Threading.Thread(delegate () {

      FormReports FormReports = new FormReports();
      FormReports.ShowDialog();
    }).Start();

    When trying to copy data from the grid (right click -> copy) error message pops up:

    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.

    In this case I can't avoid new thread when calling new form.

    How to solve it?​

     


    new System.Threading.Thread(delegate () { 
      FormReports FormReports = new FormReports();
      FormReports.ShowDialog();
    }).Start();
  2. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 28 Oct 2015 Link to this post

    Hello Dominik,

    Thank you for writing.

    It does not seem to be an issue RadGridView. When you copy data from grid, we use Clipboard.SetDataObject method. If it is called from another thread, the mentioned exception occurs. This problem can be reproduced with the default WinForms controls as well without using any Telerik component: 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            new System.Threading.Thread(delegate()
            {
                Form f = new Form();
                Button b = new Button();
                f.Controls.Add(b);
                b.Click += b_Click;
                f.ShowDialog();
            }).Start();
        }
         
        private void b_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject("test");
        }
    }

    You can find additional information on this topic on the following links:

    http://stackoverflow.com/questions/16705124/clipboard-task-error
    http://stackoverflow.com/questions/3546016/how-to-copy-data-to-clipboard-in-c-sharp

    I hope this information helps. Should you have further questions I would be glad to help.
     
    Regards,
    Dess
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top