Solved

PrivateDir and multiple applications

Posted on 1998-09-21
17
221 Views
Last Modified: 2010-08-05
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
Comment
Question by:tomcorcoran
  • 7
  • 7
  • 2
  • +1
17 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 1340443
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
 

Author Comment

by:tomcorcoran
ID: 1340444
Private Durectories are recommended for use with the BDE, see Session.PrivateDir in the BDE. Tom.
0
 
LVL 1

Expert Comment

by:Gerhard100198
ID: 1340445
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
 

Author Comment

by:tomcorcoran
ID: 1340446
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
 
LVL 1

Expert Comment

by:Gerhard100198
ID: 1340447
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1340448
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
 

Author Comment

by:tomcorcoran
ID: 1340449
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1340450
well, then try to use the sessioncomponent(TSession)

meikl
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:tomcorcoran
ID: 1340451
I do not understand what you are saying. Tom.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1340452
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
 

Author Comment

by:tomcorcoran
ID: 1340453
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1340454
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
 

Author Comment

by:tomcorcoran
ID: 1340455
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1340456
hi tom,

when it works its ok

good luck

meikl
0
 

Author Comment

by:tomcorcoran
ID: 1340457
Meiki, answer so I can grade, Tom.
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 70 total points
ID: 1340458
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1340459
oops,

i think i have it copied two times

meikl
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

19 Experts available now in Live!

Get 1:1 Help Now