C# 表格中的光标点

C# 表格中的光标点,c#,winforms,multithreading,C#,Winforms,Multithreading,我想在窗体中而不是在屏幕中获取光标点,我知道我需要使用: Point ptCursor = Cursor.Position; ptCursor = PointToClient(ptCursor); 问题是我在一个在不同线程上工作的方法中使用了它,它给了我以下错误消息: Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it

我想在窗体中而不是在屏幕中获取光标点,我知道我需要使用:

        Point ptCursor = Cursor.Position;
        ptCursor = PointToClient(ptCursor);
问题是我在一个在不同线程上工作的方法中使用了它,它给了我以下错误消息:

Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.
为什么我得到这个错误消息? 我可以在线程上运行的方法中使用这些行吗?
如何在几秒钟内调用方法在窗体线程上运行?

您需要获取窗体并调用invoke()


您需要通过
Invoke
方法访问UI级别

        Point ptCursor;

        this.Invoke(new Action(() => {
            ptCursor = Cursor.Position;
            ptCursor = PointToClient(ptCursor);
        }));


您需要在GUI线程上调度PointToClient操作:

this.Invoke(new Action(() => ptCursor = PointToClient(ptCursor)));
this.Invoke(new Action(() => ptCursor = PointToClient(ptCursor)));