🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #25

Re: Multi-threading...

#25
From: Bob Gonder
Date: Sun, 20 Jul 2008 20:31
49 lines
2409 bytes
Carl Danley wrote:

>Alright, one more question I have... Every time a user clicks my 'Check for 
>Updates' button, it creates the thread that does the checking... Should I 
>recreate that threat everytime the button is clicked, like so:

I doubt they will be clicking regularly, so, yes
one click -> one thread seems reasonable.
But, I think if the user is capable of click-click-click-clicking
your button, it might cause some problems.
Ways to get around that:

Option 1: Use a mutex within the thread so if it is started
while the mutex is active (prior thread still running),
it will simply return.

Option 2: Set a global BOOL and check it in the OnClick.
OnClick()
{	if( ! Downloading )
	{	CloseHandle(CreateThread());
}	}
Thread()
{	Downloading = TRUE;
	{do the download}
	Downloading = FALSE;
}

Option 3: Disable the button.
Button1::OnClick()
{	Button1->Enabled = FALSE;
	CloseHandle(CreateThread());
}
Thread()
{	
	{download}
	Button1->Enabled = TRUE;
}

Problem is in remembering to close the handle, and
having the handle available for waiting in the OnClose()

Something else you could do...
Have the OnClose set the global terminating
flag and then exit (no waiting for the threads).
Have the thread check the flag each time it
tries to use the window handle (ignore GUI if terminating).

	if( ! Terminating )
		Button1->Enabled = TRUE;
Technical Details
Message-ID: <648784ho0c4cpb14jgk03nn5ksd47vb8f1@4ax.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <4882cf55$1@newsgroups.borland.com> <g8o684tvboeu25bfol5rpb9ssq6oaqosmb@4ax.com> <48837d40$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,b069f0aa9205a142
X-Google-Attributes: gid10cc67,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: Bob Gonder <none@nowhere.invalid>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Multi-threading...
Date: Sun, 20 Jul 2008 13:31:20 -0700
Message-ID: <648784ho0c4cpb14jgk03nn5ksd47vb8f1@4ax.com>
References: <4882cf55$1@newsgroups.borland.com> <g8o684tvboeu25bfol5rpb9ssq6oaqosmb@4ax.com> <48837d40$1@newsgroups.borland.com>
X-Newsreader: Forte Agent 1.93/32.576 English (American)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 67.77.144.209
X-Trace: newsgroups.borland.com 1216585877 67.77.144.209 (20 Jul 2008 13:31:17 -0700)
Lines: 51
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
X-Original-Bytes: 2253