ThreadStart,字段初始化程序无法引用非静态字段

时间:2015-09-30 10:10:14

标签: c# multithreading mouseclick-event

我正在尝试在我的应用程序中使用线程但我收到错误。我在MSDN和其他论坛搜索了一个解决方案,但我没有得到它。

public class ClickingThread
    {
        private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        private const int MOUSEEVENTF_LEFTUP = 0x0004;

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        public ClickingThread() 
        {
        }

        public void MouseClicking()
        {
            int X = Cursor.Position.X;
            int Y = Cursor.Position.Y;
            Thread.Sleep(100);
            mouse_event(MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0);
        }

        public void Click()
        {
            while (true)
            {
                MouseClicking();
            }
        }
    }

当我尝试使用此类中的方法时

ClickingThread clicker = new ClickingThread();
Thread click = new Thread(new ThreadStart(clicker.Click));

我收到错误(第二行 - > clicker.Click)“字段初始值设定项无法引用非静态字段,方法等。”

感谢您的建议。

0 个答案:

没有答案