8 Replies - 380 Views - Last Post: 23 January 2020 - 04:09 PM Rate Topic: -----

#1 Gamezdude   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 03-July 14

What does 'Invoke' mean?

Posted 23 January 2020 - 11:28 AM

What does 'Invoke' mean?

Does it simply mean, 'run it/call'?

Just the same as calling a method? (DoThis();)

Simple explanations please, preferably in your own words (I tend to find the offical documentation to in-depth where not needed) :)/>

public delegate void OnEquipmentChanged(Equipment newItem, Equipment oldItem);
	public OnEquipmentChanged onEquipmentChanged;

public void Equip (Equipment newItem)
	{
		int slotIndex = (int)newItem.equipSlot;

        Equipment oldItem = Unequip(slotIndex);

		if (onEquipmentChanged != null)
		{
			onEquipmentChanged.Invoke(newItem, oldItem);
		}

		currentEquipment[slotIndex] = newItem;
        AttachToMesh(newItem, slotIndex);
	}

This post has been edited by Gamezdude: 23 January 2020 - 11:29 AM


Is This A Good Question/Topic? 0
  • +

Replies To: What does 'Invoke' mean?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15506
  • View blog
  • Posts: 62,083
  • Joined: 12-June 08

Re: What does 'Invoke' mean?

Posted 23 January 2020 - 12:09 PM

Invoke is usually called from a background thread where it asks the UI form's thread to do something when it has a minute.

Quote

The Invoke method searches up the control's parent chain until it finds a control or form that has a window handle

https://docs.microso...etframework-4.8
Was This Post Helpful? 0
  • +
  • -

#3 Gamezdude   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 03-July 14

Re: What does 'Invoke' mean?

Posted 23 January 2020 - 12:16 PM

So...I am right? Its basically an instruction to perform the prefixed duty?

This post has been edited by Skydiver: 23 January 2020 - 03:03 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15506
  • View blog
  • Posts: 62,083
  • Joined: 12-June 08

Re: What does 'Invoke' mean?

Posted 23 January 2020 - 12:18 PM

You are ignoring the whole threading part, but yeah, sure.
Was This Post Helpful? 0
  • +
  • -

#5 Gamezdude   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 03-July 14

Re: What does 'Invoke' mean?

Posted 23 January 2020 - 12:36 PM

I'll be honest, I dont know what threading is. Hence why I was ignoring it then ://>

This post has been edited by Skydiver: 23 January 2020 - 03:04 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15506
  • View blog
  • Posts: 62,083
  • Joined: 12-June 08

Re: What does 'Invoke' mean?

Posted 23 January 2020 - 02:12 PM

Well good thing you can search up on that topic.

It is well worth it the investigation.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7244
  • View blog
  • Posts: 24,556
  • Joined: 05-May 12

Re: What does 'Invoke' mean?

Posted 23 January 2020 - 03:06 PM

Gamezdude: I know it's been a while since you last posted in this forum, but please recall that there is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.

In the context of that delegate in post #1, that use of Invoke() is just to actually call the method. There is no threading involved. These two are effectively the same:
onEquipmentChanged.Invoke(newItem, oldItem);
onEquipmentChanged(newItem, oldItem)


Was This Post Helpful? 0
  • +
  • -

#8 Gamezdude   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 03-July 14

Re: What does 'Invoke' mean?

Posted 23 January 2020 - 03:51 PM

Sorry Skydiver, habit. I usually do it for context reasons and as a pointer of who im talking to. But dems the rules. :)

Nice simple answer! :D

I understand the below is the same...but why do both methods exist? It kinda seems abit pointless to me...like you can paint a picture with a brush or a knife (Bob Ross reference) but they have their uses. Would the difference here have any...uses?
onEquipmentChanged.Invoke(newItem, oldItem);
onEquipmentChanged(newItem, oldItem)


Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7244
  • View blog
  • Posts: 24,556
  • Joined: 05-May 12

Re: What does 'Invoke' mean?

Posted 23 January 2020 - 04:09 PM

Syntactic sugar.

These are the same, but there are reasons for using a for loop or switch to show a particular intent, as opposed to a while loop or if-else if-else.
for (int i = 0; i < 10; i++)
{
    Console.WriteLine(i);
}


vs.
int i = 0;
while (i < 10)
{
    Console.WriteLine(i);
    i++;
}



switch (i)
{
case 1:
    Console.WriteLine("One");
    break;

case 2:
    Console.WriteLine("Two");
    break;

default:
    Console.WriteLine("Unknown");
    break;
}


vs.
if (i == 1)
{
    Console.WriteLine("One");
}
else if (i == 2)
{
    Console.WriteLine("Two");
}
else
{
    Console.WriteLine("Unknown");
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1