Windows活动字段中的进度条? 我编写了一个C++程序,它执行耗时的计算,我希望用户能够看到程序在后台运行时的进度(最小化)。

Windows活动字段中的进度条? 我编写了一个C++程序,它执行耗时的计算,我希望用户能够看到程序在后台运行时的进度(最小化)。,c++,progress-bar,progress,C++,Progress Bar,Progress,我希望在下载文件时使用与chrome相同的效果: 如何访问此功能?我可以在C++程序中使用它吗?< /p> < P>如果在循环内执行耗时的操作,并且取决于它是否是一个计数控制循环,那么你就可以使用线程< /C> >和原子< /代码>来解决你的问题。 如果处理器体系结构支持多线程,则可以使用线程并发运行计算。线程的基本用途是与主线程并行运行函数,这些操作可以有效地同时执行,这意味着您可以使用主线程检查耗时计算的进度。并行线程带来了数据竞争的问题,如果两个线程试图访问或编辑相同的数据,它们可能会错

我希望在下载文件时使用与chrome相同的效果:


如何访问此功能?我可以在C++程序中使用它吗?< /p> < P>如果在循环内执行耗时的操作,并且取决于它是否是一个计数控制循环,那么你就可以使用<代码>线程< /C> >和<代码>原子< /代码>来解决你的问题。 如果处理器体系结构支持多线程,则可以使用线程并发运行计算。线程的基本用途是与主线程并行运行函数,这些操作可以有效地同时执行,这意味着您可以使用主线程检查耗时计算的进度。并行线程带来了数据竞争的问题,如果两个线程试图访问或编辑相同的数据,它们可能会错误地访问或编辑相同的数据,并损坏内存。这可以通过
原子
解决。您可以使用
atomic\u int
来确保两个操作不会导致数据竞争

一个可行的例子:

#include <thread>
#include <mutex>
#include <atomic>
#include <iostream>

//function prototypes
void foo(std::mutex * mtx, std::atomic_int * i);

//main function
int main() {
    //first define your variables
    std::thread bar;
    std::mutex mtx;
    std::atomic_int value;
    //store initial value just in case
    value.store(0);


    //create the thread and assign it a task by passing a function and any parameters of the function as parameters of thread
    std::thread functionalThread;
    functionalThread = std::thread(foo/*function name*/, &mtx, &value/*parameters of the function*/);

    //a loop to keep checking value to see if it has reached its final value

    //temp variable to hold value so that operations can be performed on it while the main thread does other things
    int temp = value.load();

    //double to hold percent value
    double percent;
    while (temp < 1000000000) {
        //calculate percent value
        percent = 100.0 * double(temp) / 1000000000.0;

        //display percent value
        std::cout << "The current percent is: " << percent << "%" << std::endl;

        //get new value for temp
        temp = value.load();
    }
    //display message when calculations complete
    std::cout << "Task is done." << std::endl;

    //when you join a thread you are essentially waiting for the thread to finish before the calling thread continues
    functionalThread.join();

    //cin to hold program from completing to view results
    int wait;
    std::cin >> wait;

    //end program
    return 0;
}

void foo(std::mutex * mtx, std::atomic_int * i) {
    //function counts to 1,000,000,000 as fast as it can
    for (i->store(0); i->load() < 1000000000; i->store(i->load() + 1)) {
        //keep i counting
        //the first part is the initial value, store() sets the value of the atomic int
        //the second part is the exit condition, load() returns the currently stored value of the atomic
        //the third part is the increment
    }

}
#包括
#包括
#包括
#包括
//功能原型
voidfoo(std::mutex*mtx,std::atomic_int*i);
//主要功能
int main(){
//首先定义变量
螺纹杆;
std::互斥mtx;
std::原子整数值;
//存储初始值以防万一
值。存储(0);
//创建线程并通过将函数和函数的任何参数作为线程的参数传递给它来为其分配任务
std::线程函数读取;
functionalThread=std::thread(函数的foo/*函数名*/,&mtx,&value/*参数*/);
//一个循环,用于不断检查值,以查看其是否已达到最终值
//temp变量来保存值,以便在主线程执行其他操作时可以对其执行操作
int temp=value.load();
//双击以保持百分比值
百分之二;
而(温度<100000000){
//计算百分比值
百分比=100.0*双倍(温度)/100000000.0;
//显示百分比值
std::cout load()+1)){
//我继续数数
//第一部分是初始值,store()设置原子int的值
//第二部分是退出条件,load()返回原子的当前存储值
//第三部分是增量
}
}

这称为“任务栏进度指示器”;它作为一项新功能添加到Windows 7中,并通过
ITaskbarList3
界面公开。在Windows 7中添加了很多功能。@JonathanPotter完美!我在搜索它时可能使用了错误的术语。我很好奇你认为这个答案与这个问题有什么关联?重读这个问题后,我似乎误解了答案问题