C# 如何使用BeginInvoke
我遇到了这个问题: 第20行(LOOT_FromContainer(container))我需要它作为C# 如何使用BeginInvoke,c#,begininvoke,C#,Begininvoke,我遇到了这个问题: 第20行(LOOT_FromContainer(container))我需要它作为调用,因为我想要执行的过程需要一些时间,所以ServerMessageHandler不会处理它 如果我只是简单地将该行重写为LOOT\u FromContainer.BeginInvoke(container);,那么我有以下错误: 错误CS0119:“LOOT.LOOT_FromContainer(Phoenix.Serial)”是一个“方法”, 在给定上下文中无效 我是C#的新手,来自PHP
调用
,因为我想要执行的过程需要一些时间,所以ServerMessageHandler不会处理它
如果我只是简单地将该行重写为LOOT\u FromContainer.BeginInvoke(container);
,那么我有以下错误:
错误CS0119:“LOOT.LOOT_FromContainer(Phoenix.Serial)”是一个“方法”,
在给定上下文中无效
我是C#的新手,来自PHP,关于Invoke
我真的不太了解。我已经试着整理了几天了,连谷歌都帮不上忙
[ServerMessageHandler(0x3C)]
public CallbackResult ContainerContains(byte[] data, CallbackResult prevResult)
{
PacketReader reader = new PacketReader(data);
reader.Skip(3);
ushort len = reader.ReadUInt16();
for (int i = 0; i < len; i++)
{
Serial serial = (Serial)(reader.ReadUInt32());
ushort graphic = (ushort)(reader.ReadUInt16());
reader.Skip(7);
Serial container = (Serial)(reader.ReadUInt32());
ushort color = (ushort)(reader.ReadUInt16());
if (((int)graphic == 0x0E76) && ((int)color == 0x049A))
{
LOOT_FromContainer.BeginInvoke(container);
}
}
return CallbackResult.Normal;
}
[Command]
public static void LOOT_FromContainer(Serial target)
{
UOItem lootCorpse = new UOItem(target);
if (lootCorpse.Graphic == 0x2006)
{
if (((draw == 1) && (World.Player.Backpack.AllItems.Count(draw_knife[0], draw_knife[1]) > 0)) || (World.Player.Layers[Layer.RightHand].Exist))
{
if ((lootCorpse.Amount != 400) && (lootCorpse.Amount != 401))
{
if (draw == 0)
{
UO.WaitTargetObject(lootCorpse);
UO.UseObject(World.Player.Layers[Layer.RightHand].Serial);
}
else
{
UO.WaitTargetObject(lootCorpse);
UO.UseType(draw_knife[0], draw_knife[1]);
}
UO.Wait(500);
}
}
else
{
UO.Print("Neni cim rezat, pouze lootim");
}
for (int i = 0; i < loot.Length; i++)
{
if (lootCorpse.Items.Count(loot[i][0], loot[i][1]) > 0)
{
if (loot[i][2] == 1)
{
if (loot[i][4] == 1)
{
UO.MoveItem(lootCorpse.Items.FindType(loot[i][0], loot[i][1]), 0, Aliases.GetObject("loot_bag"), loot[i][5], loot[i][6]);
UO.Wait(200);
}
else
{
UO.MoveItem(lootCorpse.Items.FindType(loot[i][0], loot[i][1]), 0, World.Player.Backpack);
UO.Wait(200);
}
}
}
}
}
}
[ServerMessageHandler(0x3C)]
公共CallbackResult容器内容(字节[]数据,CallbackResult prevResult)
{
PacketReader=新的PacketReader(数据);
读卡器。跳过(3);
ushort len=reader.ReadUInt16();
对于(int i=0;i0))| |(World.Player.Layers[Layer.RightHand].Exist))
{
如果((lootcomble.Amount!=400)和&(lootcomble.Amount!=401))
{
如果(绘图==0)
{
UO.WaitTargetObject(掠夺尸体);
UO.UseObject(World.Player.Layers[Layer.RightHand].Serial);
}
其他的
{
UO.WaitTargetObject(掠夺尸体);
UO.UseType(draw_刀[0],draw_刀[1]);
}
UO.等待(500);
}
}
其他的
{
UO.打印(“Neni cim rezat,pouze lootim”);
}
for(int i=0;i0)
{
如果(战利品[i][2]==1)
{
如果(战利品[i][4]==1)
{
UO.MoveItem(loot comble.Items.FindType(loot[i][0],loot[i][1]),0,别名.GetObject(“loot_包”),loot[i][5],loot[i][6]);
UO.等待(200);
}
其他的
{
UO.MoveItem(loot comble.Items.FindType(loot[i][0],loot[i][1]),0,World.Player.Backpack);
UO.等待(200);
}
}
}
}
}
}
我认为这就是您需要的。您需要声明一个与您的方法具有相同返回类型和输入参数的委托,实例化此委托并将其指向您的方法,然后调用BeginInvoke并传入串行变量,后跟null,null:
public delegate void LFC(Serial target);
[ServerMessageHandler(0x3C)]
public CallbackResult ContainerContains(byte[] data, CallbackResult prevResult)
{
PacketReader reader = new PacketReader(data);
reader.Skip(3);
ushort len = reader.ReadUInt16();
for (int i = 0; i < len; i++)
{
Serial serial = (Serial)(reader.ReadUInt32());
ushort graphic = (ushort)(reader.ReadUInt16());
reader.Skip(7);
Serial container = (Serial)(reader.ReadUInt32());
ushort color = (ushort)(reader.ReadUInt16());
LFC = lootfromcontainer = new LFC(LOOT_FromContainer);
if (((int)graphic == 0x0E76) && ((int)color == 0x049A))
{
lootfromcontainer.BeginInvoke(container, null, null);
//LOOT_FromContainer.BeginInvoke(container);
}
}
return CallbackResult.Normal;
}
[Command]
public static void LOOT_FromContainer(Serial target)
{
UOItem lootCorpse = new UOItem(target);
if (lootCorpse.Graphic == 0x2006)
{
if (((draw == 1) && (World.Player.Backpack.AllItems.Count(draw_knife[0], draw_knife[1]) > 0)) || (World.Player.Layers[Layer.RightHand].Exist))
{
if ((lootCorpse.Amount != 400) && (lootCorpse.Amount != 401))
{
if (draw == 0)
{
UO.WaitTargetObject(lootCorpse);
UO.UseObject(World.Player.Layers[Layer.RightHand].Serial);
}
else
{
UO.WaitTargetObject(lootCorpse);
UO.UseType(draw_knife[0], draw_knife[1]);
}
UO.Wait(500);
}
}
else
{
UO.Print("Neni cim rezat, pouze lootim");
}
for (int i = 0; i < loot.Length; i++)
{
if (lootCorpse.Items.Count(loot[i][0], loot[i][1]) > 0)
{
if (loot[i][2] == 1)
{
if (loot[i][4] == 1)
{
UO.MoveItem(lootCorpse.Items.FindType(loot[i][0], loot[i][1]), 0, Aliases.GetObject("loot_bag"), loot[i][5], loot[i][6]);
UO.Wait(200);
}
else
{
UO.MoveItem(lootCorpse.Items.FindType(loot[i][0], loot[i][1]), 0, World.Player.Backpack);
UO.Wait(200);
}
}
}
}
}
}
公共委托无效LFC(串行目标);
[ServerMessageHandler(0x3C)]
公共CallbackResult容器内容(字节[]数据,CallbackResult prevResult)
{
PacketReader=新的PacketReader(数据);
读卡器。跳过(3);
ushort len=reader.ReadUInt16();
对于(int i=0;i0))| |(World.Player.Layers[Layer.RightHand].Exist))
{
如果((lootcomble.Amount!=400)和&(lootcomble.Amount!=401))
{
如果(绘图==0)
{
UO.WaitTargetObject(掠夺尸体);
UO.UseObject(World.Player.Layers[Layer.RightHand].Serial);
}
其他的
{
UO.WaitTargetObject(掠夺尸体);
UO.UseType(draw_刀[0],draw_刀[1]);
}
UO.等待(500);
}
}
其他的
{
UO.打印(“Neni cim rezat,pouze lootim”);
}
for(int i=0;i0)
{
如果(战利品[i][2]==1)
{
如果(战利品[i][4]==1)
{
UO.MoveItem(loot comble.Items.FindType(loot[i][0],loot[i][1]),0,别名.GetObject(“loot_包”),loot[i][5],loot[i][6]);
UO.等待(200);
}
其他的
{
UO.MoveItem(loot comble.Items.FindType(loot[i][0],loot[i][1]),0,World.Player.Backpack);
UO.等待(200);
}
}
}
}
}
}
我认为这就是您所需要的。您需要声明一个与您的方法具有相同返回类型和输入参数的委托,实例化此de
LOOT_FromContainer.BeginInvoke(container);
ThreadPool.QueueUserWorkItem(LOOT_FromContainer, container);
public static void LOOT_FromContainer(object prm)
{
var target = (Serial)prm;
// ...
LOOT_FromContainer.BeginInvoke(container); //where container is a delegate that maybe declared as follows
private delegate void container; //may also contain parameters eg container(string s);