Jump to content

Several User and Bug


k0de

Recommended Posts

  • Administrators

Hi.

I run the server program. Each user has their own login and password.

But when they work at the same time, they can see each other's data. Why? And how to fix it?

 

Please give more details. How they can "see" each other's data? Where your data is stored? Which data aware components are you using? Where your data components is placed?

Link to comment
Share on other sites

I'm in the main form of writing:

if (uniEditlogin.Text ="Client1") and (uniEditPass="pass1") then

PCName="Client1";

 

if (uniEditlogin.Text ="Client2") and (uniEditPass="pass2") then

PCName="Client2";

 

if (uniEditlogin.Text ="Client3") and (uniEditPass="pass3") then

PCName="Client3";

 

In another form, there is a DBGrid and a Button that saves data from the DBGrid in *.txt. For Example, the Client saves the data in a (PCName+"\data1.txt"). Client2 saves the data in a (PCName+"\data1.txt") , etc..

But if Client1 reviews the data (loads them in the form of (PCName+"\data1.txt")) he sees the contents of Client2.

Link to comment
Share on other sites

All data that needs to be unique to a particular user session needs to be instanced for that session. The easiest way to accomplish this is to put variables that need to be unique for a particular user inside the Main module (UniMainModule). UniMainModule is instanced once for each user that logs on and only that user can see the data in his UniMainModule.

 

In your case you could make PCName a public field in UniMainModule.

Link to comment
Share on other sites

  • Administrators

What is the relationship between the order in which the module declares a variable? Why in the UniMainModule?

 

You can declare PCName in MainForm or any other Form as well, but never use a global variable. A global variable has only one instance and it is overwritten when a user assigns a new value.

Link to comment
Share on other sites

You should think of an unigui application as a multithreaded program. Each users session is a thread in one single program and just like in a multithreaded application, global variables are shared between threads.

 

When a user launches a session in a browser, your application is not completely cloned. There is only one physical version of your application running at all times. Each user just has a bit of reserved space inside your application that he has access to. That reserved space is the main form, the main module and all application forms that you launch subsequently. Each user gets his own fresh version of these forms when he starts a new session. Pretty much everything else is shared, unless you specifically instantiate it for a that user.

 

Example:

 

unit Main;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics,
 Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
 uniGUIClasses, uniGUIForm;

type
 TMainForm = class(TUniForm)
 private
   { Private declarations }
 public
   UserLocalString: string; // Each session has its own main form
                            // so each user can only see and change
                            // his own UserLocalString field.
 end;

function MainForm: TMainForm;

var
 MyGlobalString: string;  // This is a typical per unit global
                          // All users will see the same string.
                          // If one user changes it, it changes
                          // for all users.  
                        

implementation

{$R *.dfm}

uses
 uniGUIVars, MainModule;

function MainForm: TMainForm;
begin
 Result := TMainForm(UniMainModule.GetFormInstance(TMainForm));
end;

initialization
 RegisterMainFormClass(TMainForm);

end.

 

By the way, strings are not thread safe, so should never be shared between threads without proper locking mechanisms in place the make sure only one thread can access it at a time.

  • Upvote 1
Link to comment
Share on other sites

A side note:

 

Notice the difference between the unigui version of the Main form reference:

 

function MainForm: TMainForm;

 

Here is the regular VCL version:

 

var
 Form1: TForm1;

 

The unigui MainForm function is there to make sure the user gets his own main form and not some other users form.

 

If the VCL version was used, there could only every be one main form on the entire server. So when a new user logged on, the previous user would have his main form replaced with the new users main form. That would not work very well.

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...