Reputation: 7900
I want to get the cursor point in the form and not in the screen, i understand i need to use:
Point ptCursor = Cursor.Position;
ptCursor = PointToClient(ptCursor);
The issue is that i used this in a method that work on different thread, and it give me this error message:
Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.
why i get this error msg? can i used this lines in a method that run on thread? how can i call a method to run on form thread in couple of seconds?
Upvotes: 2
Views: 527
Reputation: 30115
You need to access you UI level via Invoke
method.
Point ptCursor;
this.Invoke(new Action(() => {
ptCursor = Cursor.Position;
ptCursor = PointToClient(ptCursor);
}));
http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx
http://weblogs.asp.net/justin_rogers/pages/126345.aspx
Upvotes: 1
Reputation: 10940
You need to dispatch the PointToClient operation on the GUI thread:
this.Invoke(new Action(() => ptCursor = PointToClient(ptCursor)));
Upvotes: 2
Reputation: 8008
you need to get the form and call invoke().
like
Point ptCursor = Cursor.Position;
Action action = () => ptCursor = PointToClient(ptCursor);
this.Invoke(action);
Upvotes: 0