Solved

INI files

Posted on 2002-04-02
8
234 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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
 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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