0

Hello all,

I am submitting this post because I am having problems with implementing threads in my code and wonder if someone may be able to tell me what is going on! I am using Borland C++ Builder3 to create a GUI based application. This application contains a lengthly search loop that freezes the GUI whilst it is running (None of the controls respond and if you want to exit the search before it is complete you have to use program reset). Not what I want! I am trying to run the search as a seperate thread to prevent this from happening, however the search function now does not seem to run at all and I cannot work out why!. I include below an outline of the code I am trying to use....

Disclaimer: I am relatively new to C++ and so my coding style may be less than exemplary. Any hints or tips on style are also appreciated.

//Header File...

//---------------------------------------------------------------------------
#ifndef SearchDialogH
#define SearchDialogH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <Grids.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TSearch : public TForm
{
__published:	// IDE-managed Components

//More components etc listed here.

    void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations

//More variables and functions etc listed here.

    static DWORD WINAPI ThreadFunct1(LPVOID ParamBool);
    static DWORD WINAPI ThreadFunct2(LPVOID ParamBool);

//---------------------------------------------------------------------------
extern PACKAGE TSearch *Search;
//---------------------------------------------------------------------------
#endif



//Main File...
//---------------------------------------------------------------------------
#include <vcl.h>
#include <windows.h>
#pragma hdrstop

#include "SearchDialog.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TSearch*Search;

bool ButtonClicked;

//---------------------------------------------------------------------------
__fastcall TSearch::TSearch(TComponent* Owner): TForm(Owner)
{
ButtonClicked = false;

HANDLE ThreadHandle[2];
DWORD ThreadId[2];

ThreadHandle[0] = CreateThread(NULL, 0, ThreadFunct1, this, 0, &ThreadId[0]);
if (ThreadHandle[0] == NULL)
{
    ExitProcess(3);
}


ThreadHandle[1] = CreateThread(NULL, 0, ThreadFunct2, this, 0, &ThreadId[1]);
if (ThreadHandle[1] == NULL)
{
    ExitProcess(3);
}

WaitForMultipleObjects(2, ThreadHandle, TRUE, INFINITE);

CloseHandle(ThreadHandle[0]);
CloseHandle(ThreadHandle[1]);
}


//---------------------------------------------------------------------------
DWORD WINAPI TSearch::ThreadFunct1(LPVOID)
{
//Thread to give GUI some CPU time so it can detect buttonclick etc instead of freezing.
}


//---------------------------------------------------------------------------
DWORD WINAPI TSearch::ThreadFunct2(LPVOID)
{
//Thread that concentrates on the search. The code below is an example...

for (int Count = 1; Count <= 100000000000; Count++)
{
	// code...

	if (ButtonClicked)
	{
		break;
	}
}

return 0;
}


//---------------------------------------------------------------------------
void __fastcall TSearch::Button1Click(TObject *Sender)
{
ButtonClicked = true;
}

Any help would be grately appreciated.

Many thanks,

Ben Edwards.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by mitrmkar
0

BCB version 3 is ancient, really consider updating to much more recent version, a full version is available for free, see http://cc.codegear.com/free/cppbuilder

To convince you, given that ThreadFunct1 is a member function of the form class, the following code ThreadHandle[0] = CreateThread(NULL, 0, ThreadFunct1, this, 0, &ThreadId[0]); should not even compile but apparently BCB3 happily accepts it.
The thread procedure you pass to CreateThread must be declared as: DWORD WINAPI ThreadProc(LPVOID lpParameter); meaning that you have to move the search function outside of your form class.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.