Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear friends
I am facing an error while I am reading data from clipboard on a timer my error is

ex = {"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."} 


how can i solve this problem

[edit]Just fixing the typo in the subject[/edit]
Posted
Updated 16-Apr-12 20:37pm
v2

1 solution

You need to add STAThreadAttribute on the main method:
C#
C#
[STAThread]
static void Main(string[] args)
{
}

See here for more info:
http://blogs.msdn.com/b/smondal/archive/2010/09/08/current-thread-must-be-set-to-single-thread-apartment-sta-mode-before-ole-calls-can-be-made.aspx[^]
 
Share this answer
 
Comments
VJ Reddy 17-Apr-12 1:51am    
Good answer. 5!
srilekhamenon 17-Apr-12 1:54am    
thanks but have already implemented [STAThread] flag and error is still coming my code is

private void tm_Tick(System.Object sender, System.EventArgs e)
{
try {
dynamic ms = new MemoryStream();
IDataObject data = default(IDataObject);
Image bmap = default(Image);
Sendweb(hHwnd, WM_CAP_EDIT_COPY, 0, 0);
if (this.InvokeRequired) {
this.BeginInvoke(new GetClipBord(GetIDataobject), data);
} else {
data = Clipboard.GetDataObject();
}
if (data.GetDataPresent(typeof(System.Drawing.Bitmap))) {
bmap = (Image)data.GetData(typeof(System.Drawing.Bitmap));
bmap.Save(ms, ImageFormat.Jpeg);
byteDateLine = ms.ToArray();
m_sock.Send(byteDateLine, byteDateLine.Length, 0);
}
} catch (Exception ex) {
}
}
srilekhamenon 17-Apr-12 1:56am    
iam geting error on following line
data = Clipboard.GetDataObject();
JF2015 17-Apr-12 2:03am    
Did you have a look at the answers in this forum post:
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/13b13f7f-1358-4e17-9444-ed144e354955/
Here, the exact same problem is solved. See if you find some useful hint.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900