2013-02-21 2 views
1

스레드 봇 상황을 관리하려고합니다. 나중에 액세스 (종료, 일시 중지, 변수 전달)를 위해 사전에 추가하고 싶지만 로봇을 시작한 후에도 항상 null (null 변수는 temp_bot)이됩니다.사전 스레드 lambda add

private static 
Dictionary<string, Bot> BotsOnline = new Dictionary<string, Bot>(); 
Bot temp_bot; 

new Thread(
    () => 
    { 
     int crashes = 0; 
     while (crashes < 1000) 
     { 
     try 
     { 
      temp_bot = new Bot(config, sMessage, ConvertStrDic(offeredItems), 
      ConvertStrDic(requestedItems), config.ApiKey, 
      (Bot bot, SteamID sid) => 
      { 
       return (SteamBot.UserHandler)System.Activator.CreateInstance(
       Type.GetType(bot.BotControlClass), new object[] { bot, sid }); 
      }, 
      false); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Error With Bot: " + e); 
      crashes++; 
     } 
     } 
    }).Start(); 

//wait for bot to login 

while (temp_bot == null || !temp_bot.IsLoggedIn) 
{ 
    Thread.Sleep(1000); 
} 

//add bot to dictionary 

if (temp_bot.IsLoggedIn) 
{ 
    BOTSdictionary.Add(username, temp_bot); 
} 

답변

1

새 스레드는 temp_bot 개체를 람다 식으로 전달해야하므로이 개체에 대해 알지 못합니다. 시도 :

new Thread(
    (temp_bot) => 
    { 
     int crashes = 0; 
     while (crashes < 1000) 
     { 
     try 
     { 
      temp_bot = new Bot(config, sMessage, ConvertStrDic(offeredItems), 
      ConvertStrDic(requestedItems), config.ApiKey, 
      (Bot bot, SteamID sid) => 
       { 
       return (SteamBot.UserHandler)System.Activator.CreateInstance(
       Type.GetType(bot.BotControlClass), new object[] { bot, sid }); 
       }, 
      false); 
     } 
     catch (Exception e) 
     { 
     Console.WriteLine("Error With Bot: " + e); 
     crashes++; 
     } 
    } 
    } 
).Start(); 
+0

을 내가 (동안에서 얻을하고 temp_bot == null이 || ------ 오류 선언 'SteamBot.ServiceLibrary.temp_bot' –

+0

전체 코드 'temp_bot'충돌 http://pastebin.com/cTVLTuRk –

+0

temp_bot이 클래스 변수로 저장되는 이유는 무엇입니까? StartBot() 메서드에서 볼 수있는 것만 사용되는 것 같습니다. 그래서 범위를 지정해야합니다. 그 함수에만. –