awtk/demos/demo_thread_app.c

88 lines
2.0 KiB
C
Raw Normal View History

2018-05-18 17:42:13 +08:00
/**
* File: demo1_app.c
* Author: AWTK Develop Team
* Brief: basic class of all widget
*
2019-01-07 10:58:36 +08:00
* Copyright (c) 2018 - 2019 Guangzhou ZHIYUAN Electronics Co.,Ltd.
2018-05-18 17:42:13 +08:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* License file for more details.
*
*/
/**
* History:
* ================================================================
* 2018-02-16 Li XianJing <xianjimli@hotmail.com> created
*
*/
#include "base/idle.h"
#include "base/timer.h"
#include "base/enums.h"
#include "widgets/label.h"
#include "widgets/button.h"
#include "tkc/thread.h"
#include "tkc/platform.h"
#include "tkc/mem.h"
#include "tkc/utils.h"
#include "tkc/utf8.h"
#include "widgets/window.h"
2018-05-18 17:42:13 +08:00
#include "base/image_manager.h"
#include "widgets/progress_bar.h"
2018-05-18 17:42:13 +08:00
static ret_t update_progress_bar(widget_t* progress_bar) {
static bool_t inc = TRUE;
int value = widget_get_value(progress_bar);
2018-05-20 16:35:56 +08:00
if (inc) {
2018-05-18 17:42:13 +08:00
widget_set_value(progress_bar, ++value);
} else {
widget_set_value(progress_bar, --value);
}
2018-05-20 16:35:56 +08:00
if (value > 99) {
2018-05-18 17:42:13 +08:00
inc = FALSE;
2018-05-20 16:35:56 +08:00
} else if (value < 1) {
2018-05-18 17:42:13 +08:00
inc = TRUE;
}
return RET_OK;
}
static ret_t on_timer(const timer_info_t* timer) {
2018-06-16 11:40:13 +08:00
return update_progress_bar(WIDGET(timer->ctx));
2018-05-18 17:42:13 +08:00
}
2018-06-03 10:19:34 +08:00
static ret_t on_idle(const idle_info_t* idle) {
2018-06-16 11:40:13 +08:00
return update_progress_bar(WIDGET(idle->ctx));
2018-06-03 10:19:34 +08:00
}
2018-05-18 17:42:13 +08:00
2019-05-06 16:35:51 +08:00
void* tk_thread_entry(void* args) {
2018-05-18 17:42:13 +08:00
int nr = 500;
2018-05-20 16:35:56 +08:00
while (nr-- > 0) {
2018-05-18 17:42:13 +08:00
idle_queue(on_idle, args);
timer_queue(on_timer, args, 30);
sleep_ms(30);
}
return NULL;
}
ret_t application_init() {
2019-05-06 16:35:51 +08:00
tk_thread_t* thread = NULL;
2018-05-18 17:42:13 +08:00
widget_t* progress_bar = NULL;
widget_t* win = window_create(NULL, 0, 0, 0, 0);
widget_t* label = label_create(win, 10, 10, 300, 20);
widget_set_text(label, L"Update progressbar in non GUI thread");
progress_bar = progress_bar_create(win, 10, 80, 300, 20);
2019-05-06 16:35:51 +08:00
thread = tk_thread_create(tk_thread_entry, progress_bar);
tk_thread_start(thread);
2018-05-18 17:42:13 +08:00
return RET_OK;
}