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

Copy to clipboard (different thread)

1 Answer 750 Views
GridView
This is a migrated thread and some comments may be shown as answers.
konrad
Top achievements
Rank 1
konrad asked on 28 Oct 2015, 10:04 AM

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();

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 28 Oct 2015, 02:39 PM
Hello Dominik,

Thank you for writing.

It does not seem to be an issue RadGridView. When you copy data from , 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
Tags
GridView
Asked by
konrad
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or