简体   繁体   English

在没有调试器的情况下运行时,ProgressBar会卡住

[英]ProgressBar gets 'stuck' when running without debugger

I'm working on a Visual C++ application, and part of this depackitises a file, sinjce this can take a while I implemented a progress bar to reflect the progress of the depackitisation. 我正在开发一个Visual C ++应用程序,并且其中一部分将文件解包,这可能需要一段时间才能实现一个进度条以反映该文件解包的进度。

This works fine when running with the debugger, however when I run without the debugger or from the .exe file, the progress bar consistantly gets stuck at halfway (although the program completes its function fine), and then jumps to 100%. 使用调试器运行时,此方法工作正常,但是当我不使用调试器或从.exe文件运行时,进度条始终停留在一半(尽管程序正常完成了功能),然后跳至100%。

Through some printing of the value of the progress bar I have discovered that the value IS getting set properly, but for some reason this is not being reflected visually. 通过对进度条的值进行一些打印,我发现该值已正确设置,但是由于某种原因,该值无法在视觉上反映出来。

The code for the progress bar update is 进度条更新的代码是

while (mpeg.GetProgress() < 99){
            Console::Write(this->progressBar->Value);
            this->progressBar->Value = mpeg.GetProgress();
            this->progressBar->Update();
            Sleep(100);
        }

This is done in the programs main thread. 这是在程序主线程中完成的。 The sleep stops it from updating too quickly as otherwise this is impossible to follow in the console. 睡眠会阻止它更新太快,否则将无法在控制台中进行更新。

The function which needs tracking is mpeg.Depackitise(), this is run in a seperate thread so that the progress bar can continually be updated, the code concerning the 'progress' variable is : 需要跟踪的函数是mpeg.Depackitise(),该函数在单独的线程中运行,以便可以不断更新进度条,有关“ progress”变量的代码为:

double Mpeg::GetProgress() {
return Mpeg::progress;
}

void Mpeg::SetProgress(double prog) {
Mpeg::progress = prog;
}

The follwing code is in a for loop that goes through the entire file packet by packet. 以下代码位于for循环中,该循环逐个数据包遍历整个文件。 The 'packet' variable is the current packet and 'packet_count' is the total number of packets in the file, this is all done in the seperate mpeg.Depackitise() thread. “ packet”变量是当前数据包,“ packet_count”是文件中数据包的总数,这全部在单独的mpeg.Depackitise()线程中完成。

        double Percent = 0.0;

    Percent = ((double)packet / (double)packet_count);
    SetProgress(Percent * 100);

If anyone can suggest some solutions to try it would be highly appreciated, this has been annoying me for a while now. 如果有人可以提出一些解决方案的尝试,将不胜感激,这已经让我很烦了。 Some suggestions I found online said that use of uninitialised variables could cause differences between running with/without debug, however I can't find any relevant variables that are uninitialised. 我在网上发现的一些建议说,使用未初始化的变量可能会导致在运行与不运行调试之间产生差异,但是我找不到任何未初始化的相关变量。

You must never, ever, ever block the UI thread. 您永远都不能阻止UI线程。

You're doing extensive calculations in the main thread of your application, which means its not available to answer the message pump and to draw/update drawings of controls on the screen. 您正在应用程序的主线程中进行大量计算,这意味着它无法用于回答消息泵和在屏幕上绘制/更新控件的图。

You should look into multithreading and message passing as the solution to your question. 您应该研究多线程和消息传递作为问题的解决方案。 Start a thread, do the time-consuming work there, use a callback to update the progress bar of the main thread (and beware of directly accessing the UI from anything other than the main thread!). 启动一个线程,在那儿做耗时的工作,使用回调来更新主线程的进度条(并提防从主线程以外的任何其他位置直接访问UI!)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM