hendrang Posted December 2, 2012 Posted December 2, 2012 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 Quote
Administrators Farshad Mohajeri Posted December 2, 2012 Administrators Posted December 2, 2012 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. Quote
hendrang Posted December 3, 2012 Author Posted December 3, 2012 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 Quote
hendrang Posted December 3, 2012 Author Posted December 3, 2012 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; Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.