Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21 andreas 1
/*
2
 * Copyright (C) 2021 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include "timagerefresh.h"
20
#include "terror.h"
21
 
22
TImageRefresh::TImageRefresh()
23
{
24
    DECL_TRACER("TImageRefresh::TImageRefresh()");
25
}
26
 
27
TImageRefresh::~TImageRefresh()
28
{
29
    DECL_TRACER("TImageRefresh::~TImageRefresh()");
97 andreas 30
 
31
    stopWait();
21 andreas 32
}
33
 
34
void TImageRefresh::run(const std::string& url)
35
{
36
    DECL_TRACER("TImageRefresh::run()");
37
 
38
    if (mRunning || mThread.joinable() || mSec == std::chrono::seconds(0))
39
        return;
40
 
41
    try
42
    {
43
        mStopped = false;
44
        mThread = std::thread([=] { this->_run(url); });
45
        mThread.detach();
46
    }
47
    catch (std::exception& e)
48
    {
49
        MSG_ERROR("Error starting the TImageRefresh thread: " << e.what());
50
    }
51
}
52
 
97 andreas 53
void TImageRefresh::stopWait()
54
{
55
    DECL_TRACER("TImageRefresh::stopWait()");
56
 
57
    mStopped = true;
58
 
59
    while(mRunning)
60
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
61
}
62
 
21 andreas 63
void TImageRefresh::_run(const std::string& url)
64
{
93 andreas 65
    DECL_TRACER("TImageRefresh::_run(const std::string& url)");
66
 
67
    if (mRunning)
68
        return;
69
 
21 andreas 70
    mRunning = true;
71
 
72
    while (!mStopped && !prg_stopped)
73
    {
74
        if (_callback)
75
            _callback(url);
76
 
77
        if (mStopped || mRunOnce)
78
            break;
79
 
80
        std::this_thread::sleep_for(mSec);
81
    }
82
 
83
    mRunning = false;
84
}