Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#多个线程中的操作无效:跨线程操作无效_C#_Multithreading - Fatal编程技术网

c#多个线程中的操作无效:跨线程操作无效

c#多个线程中的操作无效:跨线程操作无效,c#,multithreading,C#,Multithreading,我想更改coursor的位置,但它返回错误。请帮我更改代码。我读过关于Invoke的书,但我不知道如何使用,因为我是c#的初学者。 谢谢大家! namespace WindowsFormsApplication8 { public partial class Form1 : Form { System.Timers.Timer myTimer = new System.Timers.Timer(); public Form1()

我想更改coursor的位置,但它返回错误。请帮我更改代码。我读过关于Invoke的书,但我不知道如何使用,因为我是c#的初学者。 谢谢大家!

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        System.Timers.Timer myTimer = new System.Timers.Timer();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {            
            myTimer.Elapsed += new ElapsedEventHandler( DisplayTimeEvent );
            myTimer.Interval = 1000;
            myTimer.Start();              
        }

       public  void DisplayTimeEvent( object source, ElapsedEventArgs e )
       {
            MoveCursor();
       }

       private void MoveCursor()
       {
            this.Cursor = new Cursor(Cursor.Current.Handle);
            Cursor.Position = new Point(Cursor.Position.X - 10, Cursor.Position.Y - 10);
       }
    }
}

这是一个服务器计时器,它从UI线程触发其计时器事件。这会导致您观察到的问题,因为您必须在主UI线程上与UI交互


您需要一个UI计时器,它将在主UI线程上触发其计时器事件。例如:
System.Windows.Forms.Timer

对于您的情况,您可以尝试以下方法

namespace WindowsFormsApplication8
{
 public partial class Form1 : Form
 {
    System.Timers.Timer myTimer = new System.Timers.Timer();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {  
        myTimer.Tick += myTimer_Tick;
        myTimer.Interval = 1000;
        myTimer.Start();
    }

    void myTimer_Tick(object sender, EventArgs e)
    {      
        System.Threading.Thread.Sleep(5000); // 5000 is 5 seconds. i.e. after 5 seconds i am changing the cursor position. 
        MoveCursor();
    }

    private void MoveCursor()
    {
        this.Cursor = new Cursor(Cursor.Current.Handle);
        Cursor.Position = new Point(Cursor.Position.X - 10, Cursor.Position.Y - 10);
    }
 }
}
在WPF中,它是:

A) 将计时器更改为:DispatcherTimer-使用此计时器,您可以在tick event gui元素中进行更改

(B) 在GUI线程中调用“移动鼠标”

 this.Dispatcher.BeginInvoke((Action)(() =>
                {
                    MoveMouse();
                }));
以下是WINFORMS的有用链接:

初学者不要使用多线程。在这种情况下,这意味着使用WinForms定时器(
System.Windows.Forms.timer
)而不是
System.Timers.timer
,您可以使用搜索功能,知道吗?这个问题在这里经常被回答,所以我停止了计数……也就是说:您也可以在GUI应用程序中使用
System.Timers.Timer
,您只需确保仅从GUI线程访问GUI。是的,您可以调用主线程,但这是需要避免的额外复杂性。1)这如何解决问题?2) 为什么要睡5秒钟?1)问题只是为了改变指针的位置,代码就是这样做的,2)睡5秒钟是因为提问者想要同样的时间。(不管怎样,这只是一个暂停,以可视化位置的变化)@CodesInChaos
名称空间WindowsFormsApplication8
表明这可能不是wpfis,仅仅使用WinForms计时器不是更容易吗?我想是的,但是WinFormsStimer之前已经回答过;)所以我用谷歌搜索了另一种方式。。