循环不会更新列表框直到迭代完成

问题描述:

我有一个从列表框中获取文件名,执行system()调用,然后将该文件名移动到另一个列表框的循环。问题是,它不会一次一个地移动文件名,而是一直等到整个循环完成并一次移动它们。我该怎么做才能让它达到我想要的效果?循环不会更新列表框直到迭代完成

循环:

for each(String^% file in filename) 
{ 
    int x = convert(file); 
    lbComplete->Items->Add(lbFiles->Items[0]); // place the completed file 
    lbFiles->Items->Remove(lbFiles->Items[0]); // in the other listbox 
} 

的功能转换(),它包含了系统调用:

int convert(String^ file) 
{ 
    std::stringstream ss; 
    std::string dir, fileAddress, fileName, outputDir; 
    ... 
    return system(ss.str().c_str());   
} 
+0

这可能是更新之间缺少屏幕/小部件刷新调用的问题吗? – 2010-06-02 14:13:40

+0

看起来像共识认为如此。不知道我需要这样做,因为我从来没有真正使用过表单。猜猜我会去查找如何 – Justen 2010-06-02 14:19:00

+0

在你的循环中,你不给系统时间来重绘你的控件,如果你想在屏幕上看到它,你需要允许绘制事件在每个循环迭代之间流动 - 什么调用取决于你使用的是哪种UI(你没有提到)。 – 2010-06-02 14:24:11

您需要在循环结束时调用刷新函数,迫使它重新绘制列表框,否则它将等待循环完成。

for each(String^% file in filename) 
{ 
    int x = convert(file); 
    lbComplete->Items->Add(lbFiles->Items[0]); // place the completed file 
    lbFiles->Items->Remove(lbFiles->Items[0]); // in the other listbox 
    // INSERT REFRESH FUNCTION HERE 
} 

告诉两个列表框移动的项目后,刷新自己。

在GUI编程中,从“事件线程”或“事件循环”调用的方法需要在屏幕上显示更改之前完成,这是非常常见的。这与语言或环境无关。在我最熟悉的事物(如Java Swing)上避免这种问题的常用方法是在事件循环之外进行更新,但使用通知(swing事件)告知GUI偶尔更新显示。