所以我试图创建一个循环,在每次循环时我创建一个对象的新实例:这样的事情。
Client client = new Client(session);
client.Connect(server);
while (true) {
client.SendPacket(new Craft.Net.CreativeInventoryActionPacket(-1, new Craft.Net.ItemStack((short)19, (sbyte)1)));
client.Disconnect ("Generic");
System.Threading.Thread.Sleep(3000);
//obviously I cannot do this, because there is already an object named client.
Client client = new Client(session);
client.Connect(server);
}
我该怎么做呢?我需要创建一个MinecraftClient的新实例,在我做client.Disconnect之后我无法重用旧的实例。
答案 0 :(得分:4)
最简单的方法是改变
Client client = new Client(session);
到
client = new Client(session);
那就是说,我认为有一种更清洁的方法。从:
开始while(true) {
Client client = new Client(session);
client.Connect(server);
client.SendPacket (new Craft.Net.CreativeInventoryActionPacket(-1, new Craft.Net.ItemStack((short)19, (sbyte)1)));
client.Disconnect ("Generic");
System.Threading.Thread.Sleep(3000);
}
现在将其纳入方法:
private void ConnectAndDoStuff() {
Client client = new Client(session);
client.Connect(server);
client.SendPacket (new Craft.Net.CreativeInventoryActionPacket(-1, new Craft.Net.ItemStack((short)19, (sbyte)1)));
client.Disconnect ("Generic");
System.Threading.Thread.Sleep(3000);
}
然后:
while(true) { this.ConnectAndDoStuff() }
答案 1 :(得分:1)
每次重新开始时在循环中创建实例。
while (true) {
Client client = new Client(session);
client.Connect(server);
client.SendPacket (new Craft.Net.CreativeInventoryActionPacket(-1, new Craft.Net.ItemStack((short)19, (sbyte)1)));
client.Disconnect ("Generic");
System.Threading.Thread.Sleep(3000);
}
答案 2 :(得分:0)
如果您只想重新创建client
对象,只需删除类型定义
Client client = new Client(session);
client.Connect(server);
while (true)
{
client.SendPacket (new Craft.Net.CreativeInventoryActionPacket(-1, new Craft.Net.ItemStack((short)19, (sbyte)1)));
client.Disconnect ("Generic");
System.Threading.Thread.Sleep(3000);
client = new Client(session);
client.Connect(server);
}