请问nana::gui::timer是不是只能在form里面使用啊

Nov 11, 2012 at 9:11 AM

	class tab_page_listbox
		: public panel<false>
	{
	public:
		tab_page_listbox(window wd)
			: panel<false>(wd)
		{
			gird_.bind(*this);
			listbox_.create(*this);

			listbox_.append_header(STR("Name"), 100);
			listbox_.append_header(STR("Gender"), 60);
			listbox_.append_header(STR("Age"), 60);
			listbox_.resolver(personresolver());
			person pr;
			pr.name = STR("Peter");
			pr.gender = true;
			pr.age = 10;
			listbox_.append(0,pr);
			listbox_.append(0,pr);
			listbox_.append(0,pr);
			listbox_.append(0,pr);
			listbox_.append(0,pr);
			listbox_.append(0,pr);
			listbox_.append(0,pr);
			listbox_.append(0,pr);
			listbox_.append(0,pr);

			checkbox_.create(*this);
			checkbox_.caption(STR("Checkable Listbox"));
			checkbox_.make_event<events::click>(nana::make_fun(*this, &tab_page_listbox::_m_checked));

			gird_.add(listbox_, 0, 0);
			gird * vgird = gird_.add(10, 140);
			vgird->push(checkbox_, 0, 40);

			//nana::threads::pool_push(_m_pool, *this, &tab_page_listbox::_m_change_color)();

			timer_.make_tick(nana::make_fun(*this, &tab_page_listbox::_m_change_color));
			timer_.interval(80);
		}
	private:
		void _m_checked()
		{
			this->listbox_.checkable(this->checkbox_.checked());
		}
		void _m_change_color()
		{
			int indexex = 0;
			nana::color_t color[3] = {0xFFFF00,0x0000FF, 0xFF0000};
			for (;;)
			{
				for (size_t i=0, index = indexex; i<listbox_.size_item(); i++)
				{
					listbox_.item_foreground(0, i, color[index++]);
					if (index == 3) index = 0;
				}
				indexex ++;
				if (indexex == 3) indexex = 0;
			}
			
		}
	private:
		gird gird_;
		color_listbox		listbox_;
		checkbox	checkbox_;
		nana::threads::pool _m_pool;
		nana::gui::timer timer_;
	};

使用以上代码程序直接卡死了,如果我使用 _m_pool而不用timer就不会卡死

Coordinator
Nov 11, 2012 at 10:45 AM

这是因为_m_change_color()中有一个死循环,当触发timer的elapse事件时,它就阻塞了消息线程,因此程序卡死了。应该把for(;;)这个语句去掉,这样timer在每过80毫秒就会调用一次_m_change_color()。

如果用线程池,虽然程序不会卡死,但是它把线程池中执行它的线程给阻塞了。应该避免阻塞线程池中的线程。
Nov 12, 2012 at 2:06 PM

非常感谢,真心觉得这个库不错,有发展前途,作者加油!!