Solved

INI files

Posted on 2002-04-02
8
233 Views
Last Modified: 2010-04-04
My program seek for INI files on server and use TiniFile to change in file..
It work great, but the server don’t like it.
It jumps from 5% to 60% on my Novell server and is not good.

I need to change 2000 ini files.

How should it be done.

Wait fore file to be ready or insert a timer do delay my program..

I try the timer but most be stupid, cant get it to work…try sleep and don’t like it..

AND my StatusBar is not update if I use timer or delay.. ??  


Part of my code look like this, you can se where a need the delay..


for jw3:=0 to antal -1 do
   begin

  //  Timer1.Interval:=2000;
//    Timer1.Enabled:=True  ;
   // Sleep(500);

     IniFile := TIniFile.Create(ListBoxFiles.Items.Strings[jw3]);
     StatusBar1.Panels[1].Text:='changing : '+ListBoxFiles.Items.Strings[jw3];
     StatusBar1.Panels[0].Text:=inttostr(jw3+1) + '/' + inttostr(antal);

  if form1.Edit1.Text <> '' then
  begin
    IniFile.WriteString(form1.Edit1.Text,form1.Edit2.Text,form1.Edit3.Text);
  jw33:='ja';
  end;

………….
0
Comment
Question by:Dark_King
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6912984
Do you ever destroy the IniFile object you created?
From the code posted it seems that you create 2000 objects (which means 2000 open files) and you lose track of 1999 of the objects.
Add a IniFile.Free as last line of the loop.
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6913162
You should not call Sleep() in the gui thread. Put the work in another thread and call Sleep().
create a thread:

TMyThread=class(TThread)
  constructor Create;
  procedure Execute; override;
end;

Override the thread execute method and do an iteration of the work. only there do a Sleep().
In your thread execute method...

 while (not Terminated) do // run indefinately
 try
   DoMyThing;
   Sleep(FInterval);
   if NoMoreIniFiles then Terminate; // or something..  
 except
   if not (ExceptObject is EAbort)
     then Synchronize(ThreadException);
 end;

Choose the sleep value determined by how much you want this thread to take over the machine. The form
interacts smoothly with user.

Let me know if you need help with threads.

Regards,
Steve.
0
 
LVL 6

Author Comment

by:Dark_King
ID: 6924819
SteveWaite.

Sorry for the delay, problem with Expert suite..

I not that good on Delphi, need more exact help to fix this..

It work with sleep the wrong way, but I can’t stop the procedure if I want.
I made a button to stop my procedure, but can’t hit the button..
Sleep is only there to make a delay before change ini files, server get to much to do else.  

I want to try your example, but you have to explain more in detail..  
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6924894
Your button cannot work because you do not process the messages inside your loop.

Inside your loop you call TIniFile.Create which creates a new object.
You never call IniFile.Free in the loop so each newly created object is placed in IniFile and you lose track of the last object.
The lost object is not freed automatically so it still exists and it still has its file open.
You will end up with 1999 of 2000 open files which you cannot close.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:God_Ares
ID: 6936574
could you post cour ini code here?
0
 
LVL 3

Accepted Solution

by:
SteveWaite earned 300 total points
ID: 6938060
Here's a thread example that does not pause your gui user i/f and does not use up 100% cpu time:

type
  TMyThread = class(TThread)
  private
    { Private declarations }
    FCount: Integer;
  protected
    { Protected declarations }
    procedure DoWork;
    procedure ThreadException;
    procedure Execute; override;
  public
    { Public declarations }
    constructor Create;
  end;

  TForm1 = class(TForm)
   //etc.
  end;

var
  Form1: TForm1;
  AThread: TMyThread;

implementation

{$R *.DFM}

{TMyThread}

constructor TMyThread.Create;
begin
  inherited Create(True); // i.e. Create Suspended
  FreeOnTerminate := False;
  FCount := 0;
end;

procedure TMyThread.Execute;
begin
  while not Terminated do
  try
    DoWork;
    Sleep(1000); // here's where you sleep!
  except
    if not (ExceptObject is EAbort)
      then Synchronize(ThreadException);
  end;
end;

procedure TMyThread.DoWork;
begin
  Inc(FCount);
  if FCount > 10 then
  begin
    FCount := 0;
  end;
end;

procedure TMyThread.ThreadException;
begin
  raise Exception.Create('thread problem');
end;

{TForm1}

procedure TForm1.FormCreate(Sender: TObject);
begin
  AThread := TMyThread.Create;
  AThread.Resume;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  AThread.Free;
end;

try this out..
0
 
LVL 6

Author Comment

by:Dark_King
ID: 6941224
HEHE,, 4 minutes to find all ini file, and under 2 minutes to fix the files..
BUT THE SERVER WAS NOT HAPPY, The delay fix this, Great help SteveWaite
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6942123
Splendid!

Thanks,
Steve
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi application Soap connection 5 96
can't find the executable in Simulator 1 80
Dynamically Created Query 3 50
Multi-layered image in FireMonkey 9 19
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now