Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

PrivateDir and multiple applications

I run several applications at the same time, currently for each I set the privatedir to be a unique directory for each that I create as a sub-directory of the windows temporary diretcory. Is there a better way of doing this or is this ok?

Also, we allow the user to have the possibility of running multiple instances of the application. There's a toggle in the program to turn of multiple instances and if it's a single instance only I use a mutex to check if they have only one instance set. If they are running off a server there is not problem, but running multiple instances off a local executable is a problem as each instance seems to need a different privatedir. Has anyone any ideas how to get around this (I am developing in D 3.02 C/S)?

Thanks, Tom
0
boardtc
Asked:
boardtc
  • 7
  • 7
  • 2
  • +1
1 Solution
 
MadshiCommented:
Tom,

what do you mean with "privateDir"? I suppose you mean the "working directory"?
You can run multiple programs (local or not) with the same working dir. What problems do occur that makes you think that every instance needs an own working dir?
You CAN create a directory in the temp directory for each program. But for what purpose do you need that? Are the programs startet directly from a server? Normally (with locally installed programs) the working dir is the directory where the *.exe file is located.
If you really have to create directories in the temp directory you should make sure that they are deleted correctly, even if your programs are not correctly closed. Because a "normal" user doesn't clean the temp directory manually.

Regards, Madshi.
0
 
boardtcAuthor Commented:
Private Durectories are recommended for use with the BDE, see Session.PrivateDir in the BDE. Tom.
0
 
Gerhard100198Commented:
Tom,

Do you receive any errors when more than one instance of your app is running on the same machine?

The private dir is used by the BDE in case where temp files are to be created. An example of this is the 767687.MB which the BDE sometime create.

Since the BDE creates temp files using unique names you shouldn't have any problems with the Private dir.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
boardtcAuthor Commented:
Yes, I recive an error when the second instance is loaded on the same machine to the effect of that that directorty is busy, which does' nt seem to fit with your comments?

Thanks Tom.
0
 
Gerhard100198Commented:
1. Could you please give me the exact wording of the message?
2. If you look at what is in your private dir while an instance of the program is running, what is in there?
0
 
kretzschmarCommented:
This Problem may only occur, when you use the SessionComponent, do you ?

Use the Session Component, as follows

create a temporary Directory (Random Name) that not exists, set this path in the property PrivateDir of SessionComponent and make the Session active. All Database Components should have the Name of the SessionComponent in their property SessionName. Dont forget to delete the temporary Directory by ending your application.

A Little Sample Listing

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, DBGrids, Db, DBTables, filectrl;

type
  TForm1 = class(TForm)
    Table1: TTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    TestSession: TSession;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function randomname : String;
var
  i : Integer;
  s : String;
begin
  s := '';
  for i := 1 to 8 do
    s := s + chr(Random(25)+ 65); {Only Letters from A to Z}
  randomname := s;
end;



procedure TForm1.FormCreate(Sender: TObject);
var s : String;
begin
  s := Randomname;
  while DirectoryExists('c:\'+s) do s := Randomname;
  if createdir('c:\'+s) then
  begin
    TestSession.PrivateDir := 'c:\'+s;
    TestSession.Active := True;
    { open Database Components }
    table1.open;
  end
  else
  begin
    { Problem occured }
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  Found : Integer;
  SRec : TSearchRec;
begin
  TestSession.Active := False; {All DatabaseConnections will be closed }
  { Delete Files in temporary Directory }
  Found := FindFirst(TestSession.PrivateDir+'\*.*', faAnyFile, SRec);
  while Found = 0 do
  begin
    If SRec.Name[1] <> '.' then {Ignore Entries . and .. }
      Deletefile(TestSession.PrivateDir+'\'+SRec.Name);
    Found := FindNext(SRec);
  end;
  FindClose(SRec);
  rmdir(TestSession.PrivateDir);
end;

end.

Thats is a simple Example. Their are no Try-Except Blocks in it, so it can be that by the Filehandling raised an Exception.

I hope you can use this tip

Meikl
0
 
boardtcAuthor Commented:
Thanks for the answer, but I don't know if it'a applicable...I don't use the SessionComponent and more than setting the privatedir and netdir.

<< All Database Components should have the Name of the SessionComponent in their property.SessionName>>

I never do this - what is the purpose?

Thanks, Tom.
0
 
kretzschmarCommented:
well, then try to use the sessioncomponent(TSession)

meikl
0
 
boardtcAuthor Commented:
I do not understand what you are saying. Tom.
0
 
kretzschmarCommented:
I know the Problem with the PrivateDir and multiple Instances only from Paradox.
Where do you define the PrivateDirs for your Applications?
Do you use Paradoxfiles in your Applications?
TSession is a Component, that allows easy to set a PrivateDir at RunTime.

Meikl
0
 
boardtcAuthor Commented:
Meikl,

Yes, as I have said I use session.privatedir and session.netfiledir. I set them in my project source. I uses Paradox tables too.

Tom.
0
 
kretzschmarCommented:
tom,

well then try the listing above in a little sample application. this listing creates for each new instance an own privatedir and set its in TSession's property PrivateDir before opening.

The other Databasecomponents has a Property SessionName and this entry should have the name of the TSession Component (Easier to handle the closing of the Databaseconnect).

meikl
0
 
boardtcAuthor Commented:
Thanks yes, I got help from the borland newsgroups and implement a directory name for the privatedir now using a random number, an option I may change to is using a GUID.  As regards your code I am interested in finding out more about setting the  Database components SessionName property - I only use the default TSession, so I don't think it's necessary.

Thanks, Tom.
0
 
kretzschmarCommented:
hi tom,

when it works its ok

good luck

meikl
0
 
boardtcAuthor Commented:
Meiki, answer so I can grade, Tom.
0
 
kretzschmarCommented:
hi tom

i dropped the first rejectet answer down here


      Use the Session Component, as follows

      create a temporary Directory (Random Name) that not exists, set this path in the property
      PrivateDir of SessionComponent and make the Session active. All Database Components
      should have the Name of the SessionComponent in their property SessionName. Dont forget
      to delete the temporary Directory by ending your application.

      A Little Sample Listing

      unit Unit1;

      interface

      uses
        Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
        Grids, DBGrids, Db, DBTables, filectrl;

      type
        TForm1 = class(TForm)
          Table1: TTable;
          DataSource1: TDataSource;
          DBGrid1: TDBGrid;
          TestSession: TSession;
          procedure FormCreate(Sender: TObject);
          procedure FormClose(Sender: TObject; var Action: TCloseAction);
        private
          { Private-Deklarationen }
        public
          { Public-Deklarationen }
        end;

      var
        Form1: TForm1;

      implementation

      {$R *.DFM}

      function randomname : String;
      var
        i : Integer;
        s : String;
      begin
        s := '';
        for i := 1 to 8 do
          s := s + chr(Random(25)+ 65); {Only Letters from A to Z}
        randomname := s;
      end;



      procedure TForm1.FormCreate(Sender: TObject);
      var s : String;
      begin
        s := Randomname;
        while DirectoryExists('c:\'+s) do s := Randomname;
        if createdir('c:\'+s) then
        begin
          TestSession.PrivateDir := 'c:\'+s;
          TestSession.Active := True;
          { open Database Components }
          table1.open;
        end
        else
        begin
          { Problem occured }
        end;
      end;

      procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
      var
        Found : Integer;
        SRec : TSearchRec;
      begin
        TestSession.Active := False; {All DatabaseConnections will be closed }
        { Delete Files in temporary Directory }
        Found := FindFirst(TestSession.PrivateDir+'\*.*', faAnyFile, SRec);
        while Found = 0 do
        begin
          If SRec.Name[1] <> '.' then {Ignore Entries . and .. }
            Deletefile(TestSession.PrivateDir+'\'+SRec.Name);
          Found := FindNext(SRec);
        end;
        FindClose(SRec);
        rmdir(TestSession.PrivateDir);
      end;

      end.

      Thats is a simple Example. Their are no Try-Except Blocks in it, so it can be that by the
      Filehandling raised an Exception.

      I hope you can use this tip

      Meikl

      Use the Session Component, as follows

      create a temporary Directory (Random Name) that not exists, set this path in the property
      PrivateDir of SessionComponent and make the Session active. All Database Components
      should have the Name of the SessionComponent in their property SessionName. Dont forget
      to delete the temporary Directory by ending your application.

      A Little Sample Listing

      unit Unit1;

      interface

      uses
        Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
        Grids, DBGrids, Db, DBTables, filectrl;

      type
        TForm1 = class(TForm)
          Table1: TTable;
          DataSource1: TDataSource;
          DBGrid1: TDBGrid;
          TestSession: TSession;
          procedure FormCreate(Sender: TObject);
          procedure FormClose(Sender: TObject; var Action: TCloseAction);
        private
          { Private-Deklarationen }
        public
          { Public-Deklarationen }
        end;

      var
        Form1: TForm1;

      implementation

      {$R *.DFM}

      function randomname : String;
      var
        i : Integer;
        s : String;
      begin
        s := '';
        for i := 1 to 8 do
          s := s + chr(Random(25)+ 65); {Only Letters from A to Z}
        randomname := s;
      end;



      procedure TForm1.FormCreate(Sender: TObject);
      var s : String;
      begin
        s := Randomname;
        while DirectoryExists('c:\'+s) do s := Randomname;
        if createdir('c:\'+s) then
        begin
          TestSession.PrivateDir := 'c:\'+s;
          TestSession.Active := True;
          { open Database Components }
          table1.open;
        end
        else
        begin
          { Problem occured }
        end;
      end;

      procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
      var
        Found : Integer;
        SRec : TSearchRec;
      begin
        TestSession.Active := False; {All DatabaseConnections will be closed }
        { Delete Files in temporary Directory }
        Found := FindFirst(TestSession.PrivateDir+'\*.*', faAnyFile, SRec);
        while Found = 0 do
        begin
          If SRec.Name[1] <> '.' then {Ignore Entries . and .. }
            Deletefile(TestSession.PrivateDir+'\'+SRec.Name);
          Found := FindNext(SRec);
        end;
        FindClose(SRec);
        rmdir(TestSession.PrivateDir);
      end;

      end.

      Thats is a simple Example. Their are no Try-Except Blocks in it, so it can be that by the
      Filehandling raised an Exception.

      I hope you can use this tip

      Meikl
0
 
kretzschmarCommented:
oops,

i think i have it copied two times

meikl
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 7
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now