Jump to content

How to prevent simultaneously call a procedure/function ?


hendrang

Recommended Posts

Hi All,

 

How to prevent a procedure/function been called simultaneously by 2 or more clients ?

 

I use a global variable as a flag.

code like this :

var
 isCalled: Boolean; // Global variable

function TMainForm.UpdateData: Boolean;
begin
 if isCalled then
   Exit(False); // exit cause this function is being called by other client

 isCalled:= True; 
 .
 <my code>
 <my code>
 .
 isCalled:= False; // set isCalled to False in order other client can call this function
 Exit(True);
end;

 

It works but I think this is not safe because there is still possibility this function been called

simultaneously if this function is called at the same time by two or more clients.

 

is there any other way ?

 

Thanks in advance.

Hendra

Link to comment
Share on other sites

  • Administrators

Hi All,

 

How to prevent a procedure/function been called simultaneously by 2 or more clients ?

 

I use a global variable as a flag.

code like this :

var
 isCalled: Boolean; // Global variable

function TMainForm.UpdateData: Boolean;
begin
 if isCalled then
   Exit(False); // exit cause this function is being called by other client

 isCalled:= True; 
 .
 <my code>
 <my code>
 .
 isCalled:= False; // set isCalled to False in order other client can call this function
 Exit(True);
end;

 

It works but I think this is not safe because there is still possibility this function been called

simultaneously if this function is called at the same time by two or more clients.

 

is there any other way ?

 

Thanks in advance.

Hendra

 

Use critical sections for this.

Link to comment
Share on other sites

Use critical sections for this.

 

Hello Farshad Mohajeri,

 

Thank you for your help.

 

It works with critical sections.

 

I have another question. :)

Is it possible to reject / terminate concurrent call a procedure/function,

in this case not using critical section.

I mean the second client call for the procedure/function will be informed that

the procedure/function is being called by other client.

 

Thanks

Link to comment
Share on other sites

Hello Farshad Mohajeri,

 

Thank you for your help.

 

It works with critical sections.

 

I have another question. :)

Is it possible to reject / terminate concurrent call a procedure/function,

in this case not using critical section.

I mean the second client call for the procedure/function will be informed that

the procedure/function is being called by other client.

 

Thanks

 

Sorry Mr. Farshad Mohajeri.

 

You have given me a solution but I misunderstood it.

Problem solved. Thanks again

 

code changed :

var
 criticalSect: TRTLCriticalSection;
 isCalled: Boolean; // Global variable

function TMainForm.UpdateData: Boolean;
begin
 if isCalled then
   Exit(False); // exit cause this function is being called by other client

 EnterCriticalSection(criticalSect);  
 try
   isCalled:= True; 
   .
   <my code>
   <my code>
   .
   Exit(True);
 finally
   isCalled:= False; // set isCalled to False in order other client can call this function
   LeaveCriticalSection(criticalSect);
 end;
end;

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...