如何使用BeginInvoke

时间:2012-03-25 16:58:01

标签: c# begininvoke

我遇到了这个问题:

第20行(LOOT_FromContainer(容器)我需要将其用作Invoke,因为我想要执行的进程需要一些时间,因此ServerMessageHandler将无法处理它...

如果我只是将该行重写为LOOT_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);
                 }
              }
                }
            }
        }
  }

3 个答案:

答案 0 :(得分:1)

我认为这就是你所需要的。您需要声明一个具有与您的方法相同的返回类型和输入参数的委托,实例化该委托指向您的方法,然后调用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);
               }
            }
              }
          }
      }
}

答案 1 :(得分:1)

如果你只需要在另一个线程中运行它,你可以使用ThreadPool

为此你需要一些简单的事情

而不是:

LOOT_FromContainer.BeginInvoke(container);

你会使用:

ThreadPool.QueueUserWorkItem(LOOT_FromContainer, container);

稍微修改您的LOOT_FromContainer方法:

public static void LOOT_FromContainer(object prm)
{
   var target = (Serial)prm;
   // ...

答案 2 :(得分:1)

我假设LOOT_FromCotainer是一个控件,并且在编译器返回错误的情况下,可以粗略地猜测问题是您正在使用非委托调用BeginInvoke。 BeginInvoke的调用方式如下:

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);

所以只需按照这种方式修改代码即可。有关如何使用BeginInvoke的信息,请参阅this document