Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » MultiThreads and GLCtrl
Re: MultiThreads and GLCtrl [message #28806 is a reply to message #28735] Fri, 17 September 2010 14:36 Go to previous messageGo to previous message
ratah is currently offline  ratah
Messages: 107
Registered: July 2010
Experienced Member
Hi,

Here is an example of multithread usage.
It's a copy of code posted here (or in the tutorial of upp, i do not remember). I give it a minor modification.

I have a bug: when you push the STOP button, it crash!

#include <CtrlLib/CtrlLib.h>

using namespace Upp;

class TestThread : public TopWindow
{
	protected:
		bool stop;
		void buttonCb(void);
		Thread thr1, thr2;
		virtual void thrCb(void);
		virtual void thrPerso(void);
		
		ProgressIndicator progress;
		int32 icounter;
		Button button;
		StatusBar status;
		
	public:
		typedef TestThread CLASSNAME;

		TestThread();

};

void TestThread::thrCb(void)
{
	for(;;)
	{
		
		GuiLock __;
			
		if(progress < 100)
			progress++;
		else
			progress = 0;
		if(stop)
			break;

		Sleep(100);
	}
}

void TestThread::thrPerso(void)
{
	for(; ; )
	{
		{			
			GuiLock __;
			icounter++;
			status.Set(IntStr(icounter));
		}
		Sleep(100);
	}
}

void TestThread::buttonCb(void)
{
	if(stop)
	{
		stop = false;
		button.SetLabel("STOP");

          // I run here the first thread
		thr1.Run(THISBACK(thrCb));

          // I run here the second thread
		thr2.Run(THISBACK(thrPerso));
	}
	else
	{
		stop = true;
		thr1.Wait();
		thr2.Wait();
		button.SetLabel("START");
	}
	
	
}

TestThread::TestThread()
{
	SetRect(0, 0, 300, 150);
	Add(button);
	button.TopPos(90, 30).HCenterPos(100);
	button.SetLabel("START");
	Add(progress);
	progress.TopPos(30, 40).HCenterPos(250);
	progress.Set(0, 100);
	icounter = 0;
	AddFrame(status);
	status.Set(" ");

	stop = true;
	button <<= THISBACK(buttonCb);
		
}

GUI_APP_MAIN
{
	TestThread().Run();
}


Do not forget to change Configuration flags to Executable, GUI, Multithreaded!!

Hope you can fix the bug, good luck

RAtah
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: TreeCtrl: How to...?
Next Topic: LoadFromFile, StoreToFile and Serialize
Goto Forum:
  


Current Time: Thu May 16 12:55:17 CEST 2024

Total time taken to generate the page: 0.01456 seconds