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 » U++ Library support » U++ MT-multithreading and servers » thread sample and overrideCursor
thread sample and overrideCursor [message #29815] Fri, 19 November 2010 12:45 Go to next message
ratah is currently offline  ratah
Messages: 107
Registered: July 2010
Experienced Member
Hi everybody,

I give a little modification to thread sample i found here.

#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;
		
		Image ImgCrs;
		
	public:
		typedef TestThread CLASSNAME;

		TestThread();

};

void TestThread::thrCb(void)
{
	for(;!stop;)
	{		
		if(progress < 30)
			progress++;
		else
		{
			// I stop progress bar here
			// Inside the thread, so thr1 always run!!!!
			
			stop = false;
			OverrideCursor(ImgCrs);
			UpdateRefresh();
			break;			
		}
			
		Sleep(100);
	}
}

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

void TestThread::buttonCb(void)
{
	if(stop)
	{
		stop = false;
		button.SetLabel("STOP");
		
		ImgCrs = OverrideCursor(Image::Wait());
		thr1.Run(THISBACK(thrCb));
		thr2.Run(THISBACK(thrPerso));
	}
	else
	{
		OverrideCursor(ImgCrs);
		
		stop = true;
		thr1.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();
}


thread 1: incrementation of progress bar
thread 2: incrementation of counter in status bar


I have a question about Cursor Image,
Why image cursor doesn't change at once? I need to move it to turn back to the original arrow cursor???

Thank you for your responses

Ratah
Re: thread sample and overrideCursor [message #29816 is a reply to message #29815] Fri, 19 November 2010 14:48 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello Ratah

I am not expert in threads, but it seems you use gui inside your threads. It would be better to be in main program.


Best regards
IƱaki
Re: thread sample and overrideCursor [message #29818 is a reply to message #29816] Fri, 19 November 2010 17:17 Go to previous messageGo to next message
ratah is currently offline  ratah
Messages: 107
Registered: July 2010
Experienced Member
I use this in other program and my solution is to call PostCallback in the thread and put the code OverrideCursor(ImgCrs) inside the function.

Good WE

Re: thread sample and overrideCursor [message #29819 is a reply to message #29816] Fri, 19 November 2010 17:30 Go to previous messageGo to next message
unknown user
koldo wrote on Fri, 19 November 2010 14:48

Hello Ratah

I am not expert in threads, but it seems you use gui inside your threads. It would be better to be in main program.

It's not a problem using GUI in threads, only that he have to provide synchronization using GuiLook, which he doesn't. But as you've said, is better to use GUI only in main thread.

Ratah, see next my modifications for GUI in main thread.

#include <CtrlLib/CtrlLib.h>

using namespace Upp;

class TestThread : public TopWindow
{
    // ......
		void CheckProg() { 
			if(progress < 30)
				progress++;
			else
			{
				// I stop progress bar here
				// Inside the thread, so thr1 always run!!!!
				
				stop = false;
				OverrideCursor(ImgCrs);
				UpdateRefresh();		
			}
		}
		void CheckCount() {
			icounter++;
			status.Set(IntStr(icounter));
		}
    // ....
};

void TestThread::thrCb()
{
	for(;!stop;)
	{	
		PostCallback(THISBACK(CheckProg));
		Sleep(100);
	}
}

void TestThread::thrPerso()
{
	for(;!stop; )
	{
		PostCallback(THISBACK(CheckCount));
		Sleep(100);
	}
}


My cursor updates without moving it (Win7).
Re: thread sample and overrideCursor [message #29857 is a reply to message #29819] Tue, 23 November 2010 09:54 Go to previous message
ratah is currently offline  ratah
Messages: 107
Registered: July 2010
Experienced Member
Ok thank you for your response
Previous Topic: Httpclient timeout
Next Topic: OpenMP
Goto Forum:
  


Current Time: Fri Mar 29 05:52:46 CET 2024

Total time taken to generate the page: 0.01477 seconds