Reputation: 148
My UI is still blocking by using a backgroundworker until i use the Thread.Sleep() method. But by over 50.000 steps my programm will slow out very hard.
here is the do_work method:
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;
}
}
I try it with Application.DoEvents(). But the only way was Thred.Sleep();
Upvotes: 1
Views: 77
Reputation: 66449
The most likely reason your UI is freezing up is that you're doing something that reaches out to the main UI thread and performs a considerable amount of work on there.
This line is the most likely culprit, if it's being called very rapidly or if the Progress_Changed
event is doing a lot of work... everything in that method executes on the main thread.
worker.ReportProgress(1, new_emoticons.Count());
Comment out the above line if you don't need it, or make sure it's called less often:
if ((counter % 10) == 0)
worker.ReportProgress(1, new_emoticons.Count());
Avoid Thread.Sleep
unless you know you need it, and definitely avoid Application.DoEvents()
.
Upvotes: 1