C# Backgroundworker阻止用户界面
在我使用Thread.Sleep()方法之前,我的UI仍在使用backgroundworker进行阻塞。但是,通过超过50000个步骤,我的程序将很难慢下来 以下是do_工作方法:C# Backgroundworker阻止用户界面,c#,backgroundworker,C#,Backgroundworker,在我使用Thread.Sleep()方法之前,我的UI仍在使用backgroundworker进行阻塞。但是,通过超过50000个步骤,我的程序将很难慢下来 以下是do_工作方法: private void bw_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; List<object> arg
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;
}
}
private void bw\u DoWork(对象发送方,DoWorkEventArgs e)
{
BackgroundWorker worker=发件人作为BackgroundWorker;
列表参数=e.作为列表的参数;
分类词典安装的表情符号、抽搐表情符号、新表情符号;
int计数器=0;
sw.Restart();
installed_emoticons=参数[1]作为SortedDictionary;
开关(Convert.ToInt32(参数[0]))
{
//umwandeln SortedDictionarray中的动态twitch_表情符号
//普吕芬·韦尔奇表情画新赫伦特·沃登·穆森
案例1:
twitch_表情符号=新的SortedDictionary();
new_emoticons=new SortedDictionary();
动态din_twitch_表情=(参数[2]为动态)[“表情”];
foreach(din_twitch_表情符号中的动态新表情符号)
{
//普吕芬·沃登·索尔(abgebrochen werden soll)是一名工人
if(工作人员取消挂起)
{
e、 取消=真;
返回;
}
//动态变量信息的Zerlegen
string code=new_表情符号[“code”]。ToString();
string id=new_表情符号[“id”]。ToString();
string emoticon_set=new_emoticon[“emoticon_set”]。ToString();
//这是一种表情符号
如果(emoticon\u set==null)emoticon\u set=“0”;
//普吕芬·奥贝恩标准表情符号
if(standard_emotes.ContainsKey(code))code=standard_emotes[code];
//在ein分类中,动态表情符号的变化
如果(!twitch_emoticons.ContainsKey(代码))
twitch\u emoticons.Add(代码,新列表{@“\images\emoticons\”+emoticon\u set+“\\”+id+”.png});
其他的
twitch\u表情符号[代码]。添加(@“\images\emoticons\”+表情符号集+“\\”+id+“.png”);
//普吕芬作品中的新表情
如果(!installed_emoticons.ContainsKey(代码))
{
if(!new_emoticons.ContainsKey(代码))
新增表情符号.Add(代码,新列表{@“\images\emoticons\”+表情符号集+“\\”+id+”.png});
如果(!new\u emoticons[code]。包含(@“\images\emoticons\”+emoticon\u set+“\\”+id+“.png”))
新建表情符号[代码]。添加(@“\images\emotics\”+表情符号集+“\\”+id+“.png”);
}
如果(!installed\u emoticons[code]。包含(@“\images\emoticons\”+emoticon\u set+“\\”+id+“.png”))
{
if(!new_emoticons.ContainsKey(代码))
新增表情符号.Add(代码,新列表{@“\images\emoticons\”+表情符号集+“\\”+id+”.png});
其他的
新建表情符号[代码]。添加(@“\images\emotics\”+表情符号集+“\\”+id+“.png”);
}
计数器++;
如果((计数器%4)==0)
系统线程线程睡眠(1);
worker.ReportProgress(1,new_emotics.Count());
}
//e、 结果=空;
e、 结果=新列表{2,已安装的表情符号,twitch表情符号,新表情符号};
打破
//
案例2:
打破
}
}
我用Application.DoEvents()尝试了一下。但是唯一的办法就是睡觉 您的UI冻结的最可能原因是您正在做一些与主UI线程接触并在那里执行大量工作的事情 如果调用速度非常快,或者
Progress\u已更改事件正在做大量工作,则此行最有可能是罪魁祸首。。。该方法中的所有内容都在主线程上执行
worker.ReportProgress(1, new_emoticons.Count());
如果不需要,请注释掉上面的一行,或者确保调用次数减少:
if ((counter % 10) == 0)
worker.ReportProgress(1, new_emoticons.Count());
避免Thread.Sleep
,除非你知道你需要它,而且一定要避免Application.DoEvents()
这是太多的代码。您是否在后台访问UI?