Solved

INI files

Posted on 2002-04-02
8
232 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

12 Experts available now in Live!

Get 1:1 Help Now