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!

Aggressive application: how to create threads?

Status
Not open for further replies.

SashaBuilder3

Programmer
Jan 13, 2002
131
CA
Hi!

I developed an app which performs some lengthy calculation. While running it does not respond to any event such as window resizing, window dragging, window content repainting and so on (sorry if my terminology does not look professional - I am just a beginner). I know that creating (multiple) threads is a solution but I don't know how to do this.

Here is the project (very simplified version of the real program) created in Borland C Builder 3. I placed a panel on the form and a button on the panel. Clicking the button starts the whole process.

Can anyone demonstrate on this simple example how to solve the problem?


Thanks in advance!

SashaBuilder3

//***********header file ***************
#ifndef pr8H
#define pr8H
//---------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TPanel *Panel1;
TButton *btnStart;
void __fastcall btnStartClick(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//----------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------
#endif

//***********main file*************

#include <vcl.h>
#pragma hdrstop

#include &quot;pr8.h&quot;
#include <stdlib.h>

//-------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
//--------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------
void __fastcall TForm1::btnStartClick(TObject *Sender)
{
int x,y;
long i,j;

for(i=0;i<1000000;i++)
for(j=0;j<1000000;j++)
{
x=random(600);
y=random(400)+50;
Canvas->TextOut(x,y,&quot;Hello&quot;);
}

}
//---------------------------------------------------
 
Use a Timer. The i and j will stall the program until they calculate. The Timer will not stall your program, all the program functions will work while the timer is running.

Bob
 
Thanks Bob!

Could you please show on the code how to do this?


Sasha
 
For Builder3:
This is a simple little Timer program I designed.
It plays a sound file when the time is up.

Drop a Timer on a form.

In the Object Inspector set the following:
Enabled: true
Interval: 1000
Name: Timer1
Tag: 0

OnTimer:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <cstring.h>
#include <string.h>
#include <mmsystem.h>
#include <stdio.h>
#pragma hdrstop

#include &quot;Unit1.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link &quot;cspin&quot;
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
char sound1[160] = &quot;phone.wav&quot;;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
Edit2->Clear();
Edit2->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
if (OpenDialog1->Execute()) {
StrCopy(sound1, OpenDialog1->FileName.c_str()); }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if(RadioButton1->Checked==false) {
RadioButton2->Checked = true; Edit2->SetFocus(); }
if(Edit2->Text == &quot;&quot;) { RadioButton1->Checked = false; }
char *str1 = &quot;&quot;;
char time1[20];
char time2[20];
int comp = -6;
Edit1->Text = AnsiString(TimeToStr(Time()));

sprintf(time1, &quot;%s&quot;, Edit1->Text.c_str());
sprintf(time2, &quot;%s PM&quot;, Edit2->Text.c_str());

if(RadioButton1->Checked==true) {
comp = CompareStr(time1, time2); }
if(comp >= 0) {
sndPlaySound(sound1, SND_SYNC); }
if(Button2->OnClick) { StrCopy(time2, str1); }
}
//---------------------------------------------------------------------------

I hope that helps you.
Bob
 
You could also do Application->ProcessEvents. You should disable any buttons that you do not the user to be able to use during the running of your program. 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.
 
Hi James,

Thanks for your response. Well, Application->ProcessEvents doesn't seem to work in my case. On compilation I'm getting a message that ProcessEvents is not a member of TApplication. I could not find the method among the Help topics, are you sure Borland Builder has such a thing?


Sasha
 
Opps, try
Code:
Application->ProcessMessages();
. This is a case where my notes say one thing and my program says another. 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.
 
James,

I tried this before, with this method the window is movable and sizable but still I cannot, for example, shut it down (or do some other things in the real application) unless asking the task manager to do this dirty job. Looks like I still need to do something with threads. Can you help with this?

Thanks anyway!

Alexandre.
 
Start with faq101-1286. Also look at . 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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top