C# 后台工作在Foreach函数中不起作用?
我的应用程序向选中列表框中的选定组发送消息。我想在程序发送消息时使用C# 后台工作在Foreach函数中不起作用?,c#,multithreading,foreach,progress-bar,backgroundworker,C#,Multithreading,Foreach,Progress Bar,Backgroundworker,我的应用程序向选中列表框中的选定组发送消息。我想在程序发送消息时使用Progressbar foreach (object item in checkedListBox1.CheckedItems) { if (item.ToString() == gi.name) //don't read { if (txtBoxPath.Text == string.Empty) //don't read {
Progressbar
foreach (object item in checkedListBox1.CheckedItems)
{
if (item.ToString() == gi.name) //don't read
{
if (txtBoxPath.Text == string.Empty) //don't read
{
try
{
Dictionary<string, object> args = new Dictionary<string, object>();
args["message"] = txtBoxStatus.Text;
fb.Post(gi.id + "/feed", args);
sCount = checkedListBox1.CheckedItems.Count;
for (int i = 0; i < sCount; i++)
{
backgroundWrkSendPOST.ReportProgress(100 / sCount); //****HERE****
}
}
catch (FacebookApiException ex)
{
MessageBox.Show("ERRO:" + Environment.NewLine + ex.Message, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
.......
foreach(checkedListBox1.CheckedItems中的对象项)
{
if(item.ToString()==gi.name)//不读取
{
if(txtBoxPath.Text==string.Empty)//不读取
{
尝试
{
Dictionary args=新字典();
args[“message”]=txtBoxStatus.Text;
fb.Post(gi.id+“/feed”,args);
Scont=checkedListBox1.CheckedItems.Count;
对于(int i=0;i
其中:Scont(int):表示选中列表框中选中项目的数量。
因此,遵循数学规则,对于选定的每个项目{y}(foreach函数中的项目),应该为100%/{y}倍增,这意味着最终结果应该为100%。但是foreach函数似乎不读取工作的背景,也不进行乘法
调试时的输出:
有人能帮我吗?这个进度条最后应该保持在100%。如果我做对了,你要检查两个项目(
scont=2
),然后循环两次(I
sCount = checkedListBox1.CheckedItems.Count;
// Supposing that you could access your ProgressBar in this point
pBar.Maximum = sCount;
pBar.Minimum = 0;
for (int i = 0; i < sCount; i++)
{
backgroundWrkSendPOST.ReportProgress(i);
}