0

So I'm having a big problem on relocating my object in C# and this pops out when I try to run it (You may see some unknown words because it is on Serbian):

System.InvalidOperationException: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on. at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
at System.Windows.Forms.Control.SetBounds(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
at System.Windows.Forms.Control.set_Location(Point value)
at WindowsFormsApp1.Form1.update() in G:\Soul Night fus\WindowsFormsApp1\WindowsFormsApp1\Form1.cs:line 136

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());
}
Elton Santana
  • 950
  • 3
  • 11
  • 22
Matija
  • 46
  • 8
  • I think you need to invoke `update` from dispatcher thread. Have you ever heard about dispatcher thread? – Johnny Feb 04 '19 at 17:01
  • 2
    Side note, but creating two `Random` objects in succession will use the same seed, resulting tin the same values each time you call `Next`. just use one `Random` object and call `Next` twice. – D Stanley Feb 04 '19 at 17:29
  • @Johnny I don't know how to invoke update from dispatcher thread. – Matija Feb 04 '19 at 17:31
  • @Matija could you look at this https://stackoverflow.com/questions/9673283/how-to-update-ui-from-child-tasks-in-winforms and update your `location` using that? – Johnny Feb 04 '19 at 17:37
  • @Johnny I've tried but. Idk what I am doing wrong------------------------------------------------ System.InvalidOperationException: The current SynchronizationContext may not be used as a TaskScheduler. at System.Threading.Tasks.SynchronizationContextTaskScheduler..ctor() at System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext() at WindowsFormsApp1.Form1.update() in G:\Soul Night fus\WindowsFormsApp1\WindowsFormsApp1\Form1.cs:line 147 – Matija Feb 04 '19 at 17:54

0 Answers0