2016-08-13 3 views
1

Thread.Sleep() 메서드를 사용할 때까지 백그라운드 작업자를 사용하여 내 UI가 여전히 차단됩니다. 하지만 50.000 이상의 단계를 거치면 프로그램이 매우 느려질 것입니다.Backgroundworker blocked UI

private void bw_DoWork(object sender, DoWorkEventArgs e) 
    { 
     BackgroundWorker worker = sender as BackgroundWorker; 
     List<object> arguments = e.Argument as List<object>; 
     SortedDictionary<string, List<string>> installed_emoticons, twitch_emoticons, new_emoticons; 
     int counter = 0; 
     sw.Restart(); 
     installed_emoticons = arguments[1] as SortedDictionary<string, List<string>>; 
     switch (Convert.ToInt32(arguments[0])) 
     { 
      //umwandeln dynamic twitch_emoticons in SortedDictionarray 
      //prüfen welche Emoticons neu heruntergeladen werden müssen 
      case 1: 
       twitch_emoticons = new SortedDictionary<string, List<string>>(); 
       new_emoticons = new SortedDictionary<string, List<string>>(); 
       dynamic din_twitch_emoticons = (arguments[2] as dynamic)["emoticons"]; 
       foreach (dynamic new_emoticon in din_twitch_emoticons) 
       { 
        //Prüfen ob der worker abgebrochen werden soll 
        if (worker.CancellationPending) 
        { 
         e.Cancel = true; 
         return; 
        } 
        //Zerlegen der informationen aus der dynamischen Variable 
        string code = new_emoticon["code"].ToString(); 
        string id = new_emoticon["id"].ToString(); 
        string emoticon_set = new_emoticon["emoticon_set"].ToString(); 

        //Prüfen ob das Emoticonset einen Wert enthält 
        if (emoticon_set == null) emoticon_set = "0"; 
        //Prüfen ob ein Standard Emoticon enthalten ist 
        if (standard_emotes.ContainsKey(code)) code = standard_emotes[code]; 
        //Speichern der Emoticons aus der dynmaischen Twitch Variablen in ein SortedDicitionary 
        if (!twitch_emoticons.ContainsKey(code)) 
         twitch_emoticons.Add(code, new List<string> { @"\images\emoticons\" + emoticon_set + "\\" + id + ".png" }); 
        else 
         twitch_emoticons[code].Add(@"\images\emoticons\" + emoticon_set + "\\" + id + ".png"); 

        //Prüfen ob ein neues Emoticon enthalten ist 
        if (!installed_emoticons.ContainsKey(code)) 
        { 
         if (!new_emoticons.ContainsKey(code)) 
          new_emoticons.Add(code, new List<string> { @"\images\emoticons\" + emoticon_set + "\\" + id + ".png" }); 
         else if (!new_emoticons[code].Contains(@"\images\emoticons\" + emoticon_set + "\\" + id + ".png")) 
          new_emoticons[code].Add(@"\images\emoticons\" + emoticon_set + "\\" + id + ".png");        
        } 
        else if (!installed_emoticons[code].Contains(@"\images\emoticons\" + emoticon_set + "\\" + id + ".png")) 
        { 
         if (!new_emoticons.ContainsKey(code)) 
          new_emoticons.Add(code, new List<string> { @"\images\emoticons\" + emoticon_set + "\\" + id + ".png" }); 
         else 
          new_emoticons[code].Add(@"\images\emoticons\" + emoticon_set + "\\" + id + ".png"); 
        } 
        counter++; 
        if ((counter % 4) == 0) 
         System.Threading.Thread.Sleep(1); 
        worker.ReportProgress(1, new_emoticons.Count());        
       } 
       //e.Result = null; 
       e.Result = new List<object> {2, installed_emoticons, twitch_emoticons, new_emoticons }; 
       break; 
      // 
      case 2: 

       break; 
     } 
    } 

나는 그것이 Application.DoEvents와 시도() : 여기

는 do_work 방법입니다. 하지만 유일한 방법은 Thred.Sleep()입니다.

+0

너무 많은 코드입니다. 백그라운드에서 UI에 액세스하고 있습니까? – user3185569

+0

'worker.ReportProgress (1, new_emoticons.Count());'를 제거하고 다시 시도하십시오. 아직도 얼어 붙은거야? –

답변

1

UI가 멈추는 가장 큰 이유는 주 UI 스레드에 도달하여 상당한 양의 작업을 수행한다는 것입니다.

매우 빠르게 호출되거나 Progress_Changed 이벤트가 많은 작업을 수행하는 경우이 줄이 가장 많이 발생합니다 ... 해당 메서드의 모든 항목이 주 스레드에서 실행됩니다. 위의 라인 출력

worker.ReportProgress(1, new_emoticons.Count());        

코멘트 당신이 그것을 필요로하거나하지 않은 경우 확인이 적게라고 : 당신은 당신이 그것을 필요 알지 못한다면

if ((counter % 10) == 0) 
    worker.ReportProgress(1, new_emoticons.Count()); 

Thread.Sleep을하지 않도록하고 확실히Application.DoEvents()을 피하기 .