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 » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » THISFN and Thread (In a Thread, THISFN didn't work)
THISFN and Thread [message #49625] Sun, 18 March 2018 09:38 Go to next message
sisamu is currently offline  sisamu
Messages: 2
Registered: March 2018
Location: france
Junior Member
Hello,

I'm new in using CPP.
I made this function and i don't know how to call a function of the main in my thread. I have tried THISFN but it didn't work.
I have a compilation error with the last line "'this' was not captured for this lambda function".
If i remove the last line, the program works well.

My code :
void testDlg::Btnexecuter()
{
	//desactivation des boutons temps du tri
	controlActif(false);
	double qt( queryTime.GetData()), lt(lockTime.GetData()), rs(rowsSend.GetData()), re(rowsExaminated.GetData());
	String fichierSource(btnFichier.GetLabel());
	
	//Thread pour ne pas bloquer interface graphique
	t.Run([&stop, &fichierSource, &qt, &lt, &rs, &re]
	{
		MySqlSlowLogFilter f;
		String retourFiltre;
		String fichierDestination(GetFileDirectory(fichierSource) + GetFileTitle(fichierSource) + "-filtree.log");
		retourFiltre = f.filtrerLog(fichierSource.ToStd(), fichierDestination.ToStd(), qt,lt,rs,re);
		if (retourFiltre == "OK")
		{
			PromptOK(DeQtf("Traitement terminé.\nFichier disponible dans " + fichierDestination));
		}
		else
		{
			PromptOK(DeQtf(retourFiltre));	
		}
		THISFN(testDlg::controlActif(true));		
	});
}


Thank you for your help.

[Updated on: Sun, 18 March 2018 12:02] by Moderator

Report message to a moderator

Re: THISFN and Thread [message #49638 is a reply to message #49625] Tue, 20 March 2018 08:43 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 11844
Registered: November 2005
Ultimate Member
I think this will fix it:

t.Run([=, &stop, &fichierSource, &qt, &lt, &rs, &re]
Re: THISFN and Thread [message #49639 is a reply to message #49625] Tue, 20 March 2018 10:32 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 11844
Registered: November 2005
Ultimate Member
That said, it will compile, but is likely to crash immediately:

t.Run([=, &stop, &fichierSource, &qt, &lt, &rs, &re]

fichierSource is a local variable and you are passing a reference to it to the thread. By the time thread runs, it will likely be out of scope (same is true for the rest of variables too).

Obvious fix is to pass all by value

t.Run([=] ...

Re: THISFN and Thread [message #49643 is a reply to message #49639] Tue, 20 March 2018 23:31 Go to previous message
sisamu is currently offline  sisamu
Messages: 2
Registered: March 2018
Location: france
Junior Member
Hello Mirek,

The new code is
void testDlg::Btnexecuter()
{
	//desactivation des boutons temps du tri
	controlActif(false);

	//variables
	double qt( queryTime.GetData()), lt(lockTime.GetData()), rs(rowsSend.GetData()), re(rowsExaminated.GetData());
	String fichierSource(btnFichier.GetLabel());
		
	//Thread pour ne pas bloquer interface graphique
	t.Run([=, &stop, &fichierSource, &qt, &lt, &rs, &re, &stop]
	{
		MySqlSlowLogFilter f;
		String retourFiltre;
		String fichierDestination(GetFileDirectory(fichierSource) + GetFileTitle(fichierSource) + "-filtree-" + queryTime.GetData().ToString() + "-" + lockTime.GetData().ToString() + "-" + rowsSend.GetData().ToString() + "-" + rowsExaminated.GetData().ToString() + ".log");
		retourFiltre = f.filtrerLog(fichierSource.ToStd(), fichierDestination.ToStd(), qt,lt,rs,re, stop);
		if (retourFiltre == "OK")
		{
			PromptOK(DeQtf("Traitement terminé.\nFichier disponible dans " + fichierDestination));
		}
		else
		{
			PromptOK(DeQtf(retourFiltre));	
		}
		bool p(true);
		this->controlActif(true);
	});
}

I added the equal before variables and replace THISFN by this-> and everything is going well (even variables by référence).
Great thank for your help.
Previous Topic: How to set progress in taskbar button
Next Topic: UDP Server/Client with Winsock
Goto Forum:
  


Current Time: Fri Apr 26 06:22:06 CEST 2019

Total time taken to generate the page: 0.01988 seconds