我在c#

时间:2019-02-04 16:53:43

标签: c#

所以我在C#中重定位对象时遇到了一个大问题,当我尝试运行它时会弹出该消息(您可能会看到一些未知的单词,因为它在塞尔维亚语中):

  

System.InvalidOperationException:跨线程操作无效:Control''从创建该线程的线程之外的线程访问。     在System.Windows.Forms.Control.get_Handle()
   在System.Windows.Forms.Control.SetBoundsCore(Int32 x,Int32 y,Int32 width,     Int32高度,界限指定指定)
   在System.Windows.Forms.Control.SetBounds(Int32 x,Int32 y,Int32 width,Int32     高度,界限指定指定)
  在System.Windows.Forms.Control.set_Location(点值)处
  在G:\ Soul Night fus \ WindowsFormsApp1 \ WindowsFormsApp1 \ Form1.cs:line 136中的WindowsFormsApp1.Form1.update()中

void update()
{
    var xloc = new Random();
    var yloc = new Random();
    int counter=0;
    if (character.Location.X>= enemy.Location.X - 150&&character.Location.X <= enemy.Location.X + 150) {
        if (character.Location.Y >= enemy.Location.Y - 150 && character.Location.Y <= enemy.Location.Y + 150)
        {
            if (character.Location.X < enemy.Location.X){
                ex -= 5;
            }
            else{
                ex += 5;
            }
        }
    }
    else{
        ex += xloc.Next(-10, 10);
        ey += yloc.Next(-10, 10);
        if (enemy.Location.X > 1920){
            ex -= 200;
        }
        if (enemy.Location.X < 0){
            ex += 200;
        }
        if(enemy.Location.Y > 1080){
            ey -= 200;
        }
        if (enemy.Location.Y < 0){
            ey += 200;
        }
    }
    try{
        enemy.Location = new Point(ex, ey);
    }
    catch (Exception poruka){
        MessageBox.Show(poruka.ToString());
        System.Threading.Thread.Sleep(5000);
        Application.Exit();
    }
    counter++;
    Task.Delay(800).ContinueWith((task) => update());
}

0 个答案:

没有答案