Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在c#中结束循环之前列表框项不显示?_C#_Listbox - Fatal编程技术网

为什么在c#中结束循环之前列表框项不显示?

为什么在c#中结束循环之前列表框项不显示?,c#,listbox,C#,Listbox,我创建了一个列表框,在其中我在循环中添加项目。但我只能在循环完成后才能看到所有项目。无论它是一个正在运行的应用程序,还是它正在向数据库添加一些数据,我需要在添加每个数据后显示这些数据 下面是我的代码: public void checkAutoRenewalWeekly(string keyword, string lang, int price, int provid2) { try { //here I do someth

我创建了一个列表框,在其中我在循环中添加项目。但我只能在循环完成后才能看到所有项目。无论它是一个正在运行的应用程序,还是它正在向数据库添加一些数据,我需要在添加每个数据后显示这些数据

下面是我的代码:

public void checkAutoRenewalWeekly(string keyword, string lang, int price, int provid2)
{        
    try
    {           
        //here I do something

        for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
           //Here I do something else and then add it to the list box

           logstr = DateTime.Now.ToString("HH:mm:ss") + " Renew " + keyword + 
                    " : " + pmsisdn.Trim() + " ";

           // Here I am adding items but can only see after completing 
           // the whole functionality as this method is being called from 
           // another method so after completing that function
           // I can see the items all together
           this.lsLog.Items.Insert(0, logstr); 
           lsLog.Show();                    
        }

        ds.Clear();

    }
    catch (Exception ex)
    {
        logstr = DateTime.Now.ToString("HH:mm:ss") + ex.Message;
        this.lsLog.Items.Insert(0, logstr);
        lsLog.Show();
        process_log(logstr, "err");
        logstr = ex.StackTrace;
        process_log(logstr, "err");
    }
}
public void checkAutoRenewalWeekly(字符串关键字、字符串lang、int price、int provid2)
{        
尝试
{           
//我来做点什么

对于(i=0;i
lsLog.Show()
不做您认为它可以做的事情,您需要释放UI线程足够长的时间来重新绘制屏幕,在Windows窗体应用程序中,您可以通过调用
Application.DoEvents()来执行此操作

请提供重现您问题的小代码示例。或者至少删除与问题无关的代码请检查我的更新问题。这是winforms应用程序吗?如果您想更新UI线程,请使用后台工作程序。是的,我已检查过,并且即使我仅尝试了三行数据,它仍有数据,但仍然存在问题相同。我只有在完成循环后才能看到它。谢谢你的建议。我刚刚对它进行了一点模拟。我已将此.Controls.Add(lsLog);添加到for load事件中,然后在使用Listbox添加项后调用Application.DoEvents()。现在它正在工作。非常感谢你的帮助。