Question: I have a Dog class with a method Run which is supposed to move pictures across the screen:

```
public bool Run()
{
    Point p = PictureBoxDog.Location;

    while(p.X < 530)
    {
        int movement = Randomizer.Next(0, 3);
        p.X += movement;
        PictureBoxDog.Location = p;
    }

    if (Location == 4) //Incomplete section.
        return true;
    else
        return false;
}

```

This method is called from a button click event in which 4 dog objects are created and the each object calls the Run method:

```
private void button1_Click(object sender, EventArgs e)
{
    Dog dog1 = new Dog(pictureDog1);
    Dog dog2 = new Dog(pictureDog2);
    Dog dog3 = new Dog(pictureDog3);
    Dog dog4 = new Dog(pictureDog4);

    dog1.Run();
    dog2.Run();
    dog3.Run();
    dog4.Run();
}

```

The problem is that each method executes one by one, not simultaneously. I want each method to run at the same time. If I remove the while statement, then all methods execute at the same time, but with the while loop, they execute one after another. Any suggestions on how to fix this problem are greatly appreciated. Run method without the while loop:

```
public bool Run() //Dog1.Run()
{
    Point p = PictureBoxDog.Location;

    int movement = Randomizer.Next(0, 30);
    //Location += movement;

    p.X += movement;
    PictureBoxDog.Location = p;

    if (Location == 4) //Incomplete code.
    return true;
    else
    return false;
}

```

Answer: 
Try this 

```
Dispatcher.BeginInvoke((Action) (() =>
{
     dog1.Run();
}));
Dispatcher.BeginInvoke((Action) (() =>
{
     dog2.Run();
}));
Dispatcher.BeginInvoke((Action) (() =>
{
     dog3.Run();
}));
Dispatcher.BeginInvoke((Action) (() =>
{
     dog4.Run();
}));

```

and for the While Loop as well use this

```
Dispatcher.BeginInvoke((Action) (() =>
{
    while(p.X < 530)
    {
        int movement = Randomizer.Next(0, 3);
        p.X += movement;
        PictureBoxDog.Location = p;
    }
}));

```

BeginInvoke is asynchronous; therefore, control returns immediately to the calling object after it is called. BeginInvoke returns a DispatcherOperation object that can be used to interact with the delegate when the delegate is in the event queue. The DispatcherOperation object returned by BeginInvoke can be used in several ways to interact with the specified delegate
Why don't try this?

```
Task.Factory.StartNew( () => Parallel.ForEach<Dog>(Dogs, dog=> dog.run()));

```

To use correctly this instruction you should create a list of Dog.

```
 List<Dog> Dogs = new List<Dog>();
        Dogs.Add(dog1);
        Dogs.Add(dog2);
        Dogs.Add(dog3);
        Dogs.Add(dog4);
        Task.Factory.StartNew(() => Parallel.ForEach<Dog>(Dogs, dog => dog.Run()));

```

If you don't like list:

```
Task.Factory.StartNew(() => dog1.Run());
Task.Factory.StartNew(() => dog2.Run());
Task.Factory.StartNew(() => dog3.Run());
Task.Factory.StartNew(() => dog4.Run());

```

To interact with the UI from a different thread, you need to use a delegate and calling Control.Invoke/BeginInvoke. 
You can test whether or not you need to call Invoke using the InvokeRequired property.
So in your case:

```
if (PictureBoxDog.InvokeRequired){
PictureBoxDog.Invoke((MethodInvoker)(() => PictureBoxDog.location = p));}

```