Hi,

I have recently jumped into the realms of C# and I am having some trouble with editing properies accross threads. From what i have managed to research thus far i beleive i need to use the Invoke() method, however i am not quite sure how to go about this correctly.

The Scenario:

Image1 is created by the main thread.
In another thread i have the following

Image1.Left = Image1.Left + 1

However this throws up an error, any pointers would be most appreciated.

Many Thanks,

Jack Wetson

Create a method as RamyMahrous said then invoke it using any UI thread. You can do it by control.BeginInvoke().

thank you for your responses thus far

however, is there any chance some one can show an example what goes inside the BeginInvoke(here)??

Many Thanks

Use Invoke, I think you don't consider Asynchrony to use BeginInvoke and EndInvoke..
I think you don't need an example to include your code in method!

thank you for your responses thus far

however, is there any chance some one can show an example what goes inside the BeginInvoke(here)??

Many Thanks

check this.

ok i did warn i was new to programming so ive tried the suggestions resulting in

public void InvMoveUp(int New_Y)
        {
            while (MainForm.PacMan.Top < New_Y)
            {
                if (MainForm.PacMan.InvokeRequired)
                {
                    // It's on a different thread, so use Invoke.

                    MainForm.PacMan.BeginInvoke(MoveUp);
                }
                else
                {
                    // It's on the same thread, no need for Invoke
                    MainForm.PacMan.Top = MainForm.PacMan.Top + 1;
                }



            }
        }
        public void MoveUp()
        {
            MainForm.PacMan.Top = MainForm.PacMan.Top + 1;
        }

Yet this still throws up errors, any further help would be great.

Error 1:

Error	1	The best overloaded method match for 'System.Windows.Forms.Control.BeginInvoke(System.Delegate, params object[])' has some invalid arguments	C:\Users\Freddy\Desktop\PacSurd\Surd1.cs	130	21	PacSurd

Error 2:

Error	2	Argument '1': cannot convert from 'method group' to 'System.Delegate'	C:\Users\Freddy\Desktop\PacSurd\Surd1.cs	130	49	PacSurd

Hope thats of some help

BegineInvoke takes to arguments and seems you send just one argument.
1- Send Method name without "( )"
2- If the method is parameterless pass empty array of object new object[] {}

ok my new code is

public void InvMoveUp(int New_Y)
        {
            while (MainForm.PacMan.Top < New_Y)
            {
                if (MainForm.PacMan.InvokeRequired)
                {
                    // It's on a different thread, so use Invoke.

                    MainForm.BeginInvoke(MoveUp, new object[] { });
                }
                else
                {
                    // It's on the same thread, no need for Invoke
                    MainForm.PacMan.Top = MainForm.PacMan.Top + 1;
                }



            }
        }

        public void MoveUp(object[] empty)
        {
            MainForm.PacMan.Top = MainForm.PacMan.Top + 1;
        }

yet

Error	1	The best overloaded method match for 'System.Windows.Forms.Control.BeginInvoke(System.Delegate, params object[])' has some invalid arguments	\\marling5\Users\Students\Intake02\02WETSONJ68\Computing CW\PacSurd\Surd1.cs	146	21	PacSurd

and

Error	2	Argument '1': cannot convert from 'method group' to 'System.Delegate'	\\marling5\Users\Students\Intake02\02WETSONJ68\Computing CW\PacSurd\Surd1.cs	146	42	PacSurd

Remove the object[] argument from your MoveUp() method. You don't need it.

Also, when you do the BeginInvoke, it should look like this:

MainForm.BeginInvoke(MoveUp, null);

a params object means you can have one or many objects as a parameter to pass to your method. The BeginInvoke feeds those objects to the method's signature, in case the method takes any. If the method you're invoking doesn't require any parameters, then simply use null. It's worked for me...

nope im still getting the same errors as before

code atm is

public void InvMoveUp(int New_Y)
        {
            while (MainForm.PacMan.Top < New_Y)
            {
                if (MainForm.PacMan.InvokeRequired)
                {
                    // It's on a different thread, so use Invoke.
                    MainForm.BeginInvoke(MoveUp, null);
                    
                }
                else
                {
                    // It's on the same thread, no need for Invoke
                    MainForm.PacMan.Top = MainForm.PacMan.Top + 1;
                }



            }
        }
        public void MoveUp()
        {
            MainForm.PacMan.Top = MainForm.PacMan.Top + 1;
        }

Edit: dont know if it helps but ive uploaded my files to http://www.megaupload.com/?d=I2ERERO4

Is it just me or are you guys making this awfully difficult?

Try this code:

public void InvMoveUp(int New_Y)
{
	while (MainForm.PacMan.Top < New_Y)
	{
		MainForm.PacMan.Invoke(new MoveUpDelegate(MoveUp));
	}
}

public delegate void MoveUpDelegate();
public void MoveUp()
{
	MainForm.PacMan.Top = MainForm.PacMan.Top + 1;
}

Is it just me or are you guys making this awfully difficult?

Try this code:

public void InvMoveUp(int New_Y)
{
	while (MainForm.PacMan.Top < New_Y)
	{
		MainForm.PacMan.Invoke(new MoveUpDelegate(MoveUp));
	}
}

public delegate void MoveUpDelegate();
public void MoveUp()
{
	MainForm.PacMan.Top = MainForm.PacMan.Top + 1;
}

That worked! Thank you very Much !

This question has already been answered. Start a new discussion instead.