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()");
30
}
31
 
32
void TImageRefresh::run(const std::string& url)
33
{
34
    DECL_TRACER("TImageRefresh::run()");
35
 
36
    if (mRunning || mThread.joinable() || mSec == std::chrono::seconds(0))
37
        return;
38
 
39
    try
40
    {
41
        mStopped = false;
42
        mThread = std::thread([=] { this->_run(url); });
43
        mThread.detach();
44
    }
45
    catch (std::exception& e)
46
    {
47
        MSG_ERROR("Error starting the TImageRefresh thread: " << e.what());
48
    }
49
}
50
 
51
void TImageRefresh::_run(const std::string& url)
52
{
53
    mRunning = true;
54
 
55
    while (!mStopped && !prg_stopped)
56
    {
57
        if (_callback)
58
            _callback(url);
59
 
60
        if (mStopped || mRunOnce)
61
            break;
62
 
63
        std::this_thread::sleep_for(mSec);
64
    }
65
 
66
    mRunning = false;
67
}