首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用BeginInvoke

如何使用BeginInvoke
EN

Stack Overflow用户
提问于 2012-03-25 16:58:00
回答 3查看 796关注 0票数 1

我有个问题:

第20行(LOOT_FromContainer(容器))我需要使用它作为Invoke,因为我想执行的过程需要一些时间,所以ServerMessageHandler不会处理它.

如果我只是简单地将这一行重写为LOOT_FromContainer.BeginInvoke(container);,那么我就会出现以下错误:

error CS0119:'LOOT.LOOT_FromContainer(Phoenix.Serial)‘是一个’方法‘,它在给定的上下文中无效

我是C#新手,来自PHP,对于Invoke,我不太了解。几天来我一直在努力解决这个问题,连谷歌都没帮上忙.

代码语言:javascript
复制
  [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);
                 }
              }
                }
            }
        }
  }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-25 17:06:47

我想这就是你需要的。您需要声明一个与您的方法具有相同的返回类型和输入参数的委托,实例化该委托将其指向您的方法,然后调用BeginInvoke传递您的串行变量,然后是null,null:

代码语言:javascript
复制
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);
               }
            }
              }
          }
      }
}
票数 2
EN

Stack Overflow用户

发布于 2012-03-25 17:14:27

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

为此,你需要一些简单的东西。

而不是:

代码语言:javascript
复制
LOOT_FromContainer.BeginInvoke(container);

你可以使用:

代码语言:javascript
复制
ThreadPool.QueueUserWorkItem(LOOT_FromContainer, container);

并稍微修改LOOT_FromContainer方法以:

代码语言:javascript
复制
public static void LOOT_FromContainer(object prm)
{
   var target = (Serial)prm;
   // ...
票数 2
EN

Stack Overflow用户

发布于 2012-03-25 17:33:11

我假设LOOT_FromCotainer是一个控件,考虑到编译器返回的错误,人们可以粗略地猜出问题在于您使用非委托调用BeginInvoke。BeginInvoke的名称如下:

代码语言:javascript
复制
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。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9862093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档