Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me build a thread 1

Status
Not open for further replies.

801119

Programmer
Apr 10, 2000
311
SE
Greetings all...

I'm desperat need of help with building a thread.. well, to say desperat is to exaggerate. But I do could use help setting up a thread. I have no idea on how to do this... so please guide me!? :) plz..
reading BCB help files gave me nothing, and the inbuilt example gave me less.. My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
I just made a new faq : how to build a thread. If it is not clear enough give a sign.


regards

Wim Vanherp Wim Vanherp
Wim.Vanherp@myself.com
 
Super... splendid, I found the new thread object before, but then I was stuck! Many thanks to you wimvanherp and a star...

one other thing, how do I declare global variables so that I can use them in the thread?

if I have a variable in Form1 named bool continue = true; and I want to "reach" it from TMyThread in an if statment, how do I do this? My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
IMHO, the best way to declare global variables is in the form's header under the
Code:
private:	// User declarations
section. The kicker is you can't set it up there. Once the variable is declared in the header, you could set it up in the form's start up code. For example, in FORM.H:
Code:
private:	// User declarations
    bool continue

Then in FORM.CPP:
Code:
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    continue = true; // set up here, declaration in header
}
James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
BTW, good FAQ, wimvanherp.
James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
2ffat, I tried to declare it under private, but it did me no good. As soon as I tried to reach it from TMyThread using either with or without Form1->scaninprogress, and read it's value I got this error
Code:
'TForm1::scaninprogress' is not accessible.
Is there anything else I need to do? My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
You have to declare the variable in the public: section not in the private section, if you declare it in the private section no other component can see the variable.

so


public : bool continue; Wim Vanherp
Wim.Vanherp@myself.com
 
Ok... so that worked!! I knew it could be something like that... wasn't sure tough!!

Guess what, the problems never stop X-) ...
ok, this one is a bomer!!

I can now use global variables to stop the function within the Thread.. but there is still one problem (that wasn't there earlier today) and that is, when the function within the theread is done and I close the application I get this message, and I quote:
"Project Project1.exe raised exception class EWin32Error with message 'Win32 Error. Code: 1400.
Invalid window handle'. process stopped. Use Step or Run to continue.
"
Help files gave me nothing more then what I could draw as my own conclusion... that it has something to do with Windows (as an OS?)
AND that's not all, when I use following code while the Thread is running I get the same error:
Code:
       // Add code to relocate window
        TBorderIcons tempBI = BorderIcons;
        exploreCD = !exploreCD; // exploring CD is now NOT the same as before. e.g. true before, false now
        if(exploreCD)
                {
                if(wsBulk == wsMaximized) // if current windows is normal
                        {
                         Form1->WindowState = wsBulk; // Maximize window
                        }
                  tempBI << biMaximize; // Disalow minimize
                 Form1->Constraints->MaxWidth = 0;
                 Form1->Constraints->MaxHeight = 0;
                 Form1->Constraints->MinWidth = 575;
                 Form1->Constraints->MinHeight = 325;
                 DirectoryTree->Enabled = true;
                 FileList->Enabled = true;
                }
        else
                {
                if(Form1->WindowState == wsMaximized) // if current window is maximized
                        {
                         Form1->WindowState = wsNormal;
                         wsBulk = wsMaximized;
                        }
                else    {
                         wsBulk = wsNormal;
                        }
                  tempBI >> biMaximize; // Allow minimize
                 Form1->Constraints->MaxWidth = 575;
                 Form1->Constraints->MaxHeight = 114;
                 Form1->Constraints->MinWidth = 575;
                 Form1->Constraints->MinHeight = 114;
                 DirectoryTree->Enabled = false;
                 FileList->Enabled = false;
                }
        BorderIcons = tempBI; // Set new bordericon on Form1
But changing the attributes on the form shouldn't cause this to happen, now should it?

I also tried to comment away the code under execute() in the thread file and had no problem!!

Could someone take a look at my code and let me know why?
(didn't have time to comment it, hope you can still read it)
Code:
void __fastcall Tc_searchThread::Execute()
{
        //---- Place thread code here ----
 Form1->scaninprogress = true; // bool type, global variable
 Form1->btnAbortScan->Visible = true; // SpeedButton
 struct ffblk ffblk;
 AnsiString curr_path;
 int done;
 int antal_filer=0,antal_folders=0;
 long total_size=0;
     Form1->ListBox1->Items->Clear();
     Form1->lstContents->Lines->Clear();
 int folder_num=0;
 Form1->ListBox1->Items->Add( &quot;E:&quot; );
 while(Form1->ListBox1->Items->Count > 0 && Form1->abort_scan == false) // && Form1->abort_scan == false
    {
     curr_path = Form1->ListBox1->Items->Strings[0]+&quot;\\&quot;;
     done = findfirst((AnsiString(curr_path)+&quot;*.*&quot;).c_str(), &ffblk,FA_DIREC|FA_HIDDEN|FA_RDONLY);
     while (!done && Form1->abort_scan == false) //  && Form1->abort_scan == false
        {
         Form1->StatusBar1->Panels->Items[2]->Text =&quot;Scanning &quot; +ExtractShortPathName(curr_path+&quot;\\&quot;+ffblk.ff_name);
         int direct = strcmp(ffblk.ff_name, &quot;.&quot;);
         int subdirect = strcmp(ffblk.ff_name, &quot;..&quot;);
         if(direct != 0 && subdirect != 0)
         if( (ffblk.ff_attrib&FA_DIREC) == FA_DIREC )
            {
             folder_num++;
             Form1->ListBox1->Items->Insert(1, (AnsiString(curr_path) + AnsiString(ffblk.ff_name)) );
             Form1->lstContents->Lines->Insert(0,&quot;\&quot;&quot;+(AnsiString(curr_path) + AnsiString(ffblk.ff_name)));
             antal_folders++;
            }
         else
            {
             Form1->lstContents->Lines->Add(&quot;;&quot;+curr_path+AnsiString(ffblk.ff_name)+&quot;?&quot;+ffblk.ff_fsize);
             total_size+=ffblk.ff_fsize;
             antal_filer++;
            }
         done = findnext(&ffblk);
        }
     Form1->ListBox1->Items->Delete(0);
    }
 Form1->lstContents->Lines->SaveToFile( &quot;c:\\test.dbx.txt&quot; );
 Form1->scaninprogress = false;
 Form1->abort_scan = false;
 Form1->btnAbortScan->Visible = false;
}
My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
How are you closing your thread? From the error you are getting, it looks like your thread is still running. Somewhere you may need
Code:
if (Tc_searchThread)
    Tc_searchThread->Terminate();

Or something similar. Maybe on the FormClose method? James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Are you sure that your while loop (in the thread ) has terminated when you close your main form ? As far as I can see you make references to your main form in your thread, so if this thread is still running and you close your main form, then your thread comes into troubles, that's sure.
You can try to terminate the thread in the FormClose event of your mainform like 2ffat says. If that should not work (maybe that the main form is still closed before the thread) you can put a variable in your main form like CloseThread that makes your Thread finishing (in the while loop of the thread (if (Form1->CloseThread ) break; ) and activate a timer that causes a delay to close your main application. But maybe it just works to close the thread in the formclose event. Wim Vanherp
Wim.Vanherp@myself.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top