简体   繁体   English

带线程的多个对话框中的进度条

[英]Progress bar in multiple dialogs with thread

My application has to keep track of time and alert user at different stages to allow or not allow certain actions. 我的应用程序必须跟踪时间并在不同阶段提醒用户,以允许或不允许某些操作。 Because the user will be waiting, I want to show a progress to show wait time. 因为用户将等待,所以我想显示进度以显示等待时间。 For example my device has to wait for 5 minutes when it is started before it can be ready to use. 例如,我的设备在启动后必须等待5分钟才能使用。 The problem is that user can navigate to multiple dialogs and I want to show the progress bar on 2 of these dialogs. 问题是用户可以导航到多个对话框,并且我想在其中两个对话框中显示进度条。 I used a thread to 'run the clock' and it will post messages to the GUI when progress bar needs to be updated or time activity is complete. 我使用了一个线程来“运行时钟”,当需要更新进度条或完成时间活动时,它将把消息发布到GUI。

The problem is that I can pass the handle of the initial window which can receive notifications from the thread but what happens when user go to the other screen? 问题是我可以传递初始窗口的句柄,该窗口可以接收来自线程的通知,但是当用户转到另一个屏幕时会发生什么? This other window don't even exist yet. 这个其他窗口甚至还不存在。 Is there any other mechanism I can use? 我还可以使用其他机制吗? My goal is to to reuse the time-check code in both the dialogs. 我的目标是在两个对话框中重用时间检查代码。

A very simplified code is below: 下面是一个非常简化的代码:

UINT MonitorTimeThread(LPVOID pVoid )
{
    HWND hWnd = ( HWND ) pVoid;

    HANDLE timer = ::CreateWaitableTimer(NULL, FALSE, NULL);

    SYSTEMTIME t;
    ::GetSystemTime(&t);

    LARGE_INTEGER alarm;
    ::SystemTimeToFileTime(&t, (FILETIME *)&alarm);
    ::SetWaitableTimer(timer, &alarm, 5000, NULL, NULL, FALSE);

    bool done = false;

    while (!done )
    {

        if (WaitForSingleObject( timer, INFINITE ) == WAIT_OBJECT_0 )
        {
            TRACE("time inteval has passed\r\n");

            // update the progress bar on GUI
            PostThreadMessage(hWnd, UM_PROGRESS_TICK, 0 );

            // if user has switch to another window, how to direct messages to that?
        }
    }

    return 0;
}

I'm not sure if I'm a victim of the " If all you have is a hammer everything looks like a nail " syndrome but you may use a similar solution to https://stackoverflow.com/a/17033971/145757 我不确定我是否是“ 如果您只有锤子,一切看起来都像钉子 ”综合症的受害者,但是您可以使用与https://stackoverflow.com/a/17033971/145757类似的解决方案

class MyTwoWindows
{
    public: HWND window1;
    public: HWND window2;

    public: MyTwoWindows(HWND w1, HWND w2)
    {
        this->window1 = w1;
        this->window2 = w2;
    }
};

Then I don't know how you transmit the data to the MonitorTimeThread function but instead of a single HWND you could pass an instance of MyTwoWindows . 然后我不知道如何将数据传输到MonitorTimeThread函数,但是可以传递MyTwoWindows的实例来代替单个HWND

UINT MonitorTimeThread(LPVOID pVoid )
{
    MyTwoWindows* data = (MyTwoWindows*) pVoid;
    ...
    PostThreadMessage(data->window1, UM_PROGRESS_TICK, 0 );
    PostThreadMessage(data->window2, UM_PROGRESS_TICK, 0 );

Moreover if the two windows are not created at the same time you can still modify the object live: 此外,如果没有同时创建两个窗口,则仍可以实时修改对象:

void CreateSecondWindow()
{
    HWND hwnd = // a new window;
    myTwoWindows->window2 = hwnd;
}

And in the monitoring function: 并在监视功能中:

if (data->window2 != NULL)
{
    PostThreadMessage(data->window2, UM_PROGRESS_TICK, 0 );
}

Sorry if this is irrelevant but it's late. 抱歉,这无关紧要,但是已经晚了。 :) :)

PS: I've written about this syndrome here: http://pragmateek.com/if-all-you-have-is-a-hammer/ (implemented in C# but good for C++ programmers too) ;) PS:我在这里写过关于这种综合症的文章: http : //pragmateek.com/if-all-you-have-is-a-hammer/ (在C#中实现,但也对C ++程序员也有帮助)

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

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