Synchronization Require inStatic Function Call!
-
well here is the description of my problem I have a component in which all the methods are static let the skeleton is like below public Class Component { ............. ......... ....... public static void St_F1(--,--,--) { --- - --- -- } public static void St_F2(--,--,--) { --- - --- -- } public static void St_F3(--,--) { --- - --- -- } public static void St_F4(--) { --- - --- -- } } ////////////////// now these functions are called in other class MyClass in the delegate MyDlg that is called when the Capture event occurs public Class MyClass { ---- ---- private Scanner myobj=new Scanner(); myobj.Capture+=new CaptureEventHandler(MyDlg); private void MyDlg(---,--,---) { Componet.St_F4(--); Componet.St_F1(--,--,---); if(myobj.id==1) Componet.St_F3(---,--); if(myobj.id==2) Componet.St_F2(--,--); } } //////////////////////// Now i make 2 objects of MyClass each of them has a unique value of myobj.id MyClass ClassObj1=new MyClass(); MyClass ClassObj2=new MyClass(); Now both of these instances of the MyClass have to call event myobj.Capture+=new CaptureEventHandler(MyDlg); implicitly nad may be event occurs from both objects at a time i want to make the call to the each Static Function called in the body of MyDlg synchronized. So as Only one object may call the Static function at a time. How to achieve this. Plz help Me Thnx in Advance
-
well here is the description of my problem I have a component in which all the methods are static let the skeleton is like below public Class Component { ............. ......... ....... public static void St_F1(--,--,--) { --- - --- -- } public static void St_F2(--,--,--) { --- - --- -- } public static void St_F3(--,--) { --- - --- -- } public static void St_F4(--) { --- - --- -- } } ////////////////// now these functions are called in other class MyClass in the delegate MyDlg that is called when the Capture event occurs public Class MyClass { ---- ---- private Scanner myobj=new Scanner(); myobj.Capture+=new CaptureEventHandler(MyDlg); private void MyDlg(---,--,---) { Componet.St_F4(--); Componet.St_F1(--,--,---); if(myobj.id==1) Componet.St_F3(---,--); if(myobj.id==2) Componet.St_F2(--,--); } } //////////////////////// Now i make 2 objects of MyClass each of them has a unique value of myobj.id MyClass ClassObj1=new MyClass(); MyClass ClassObj2=new MyClass(); Now both of these instances of the MyClass have to call event myobj.Capture+=new CaptureEventHandler(MyDlg); implicitly nad may be event occurs from both objects at a time i want to make the call to the each Static Function called in the body of MyDlg synchronized. So as Only one object may call the Static function at a time. How to achieve this. Plz help Me Thnx in Advance
Does locking/Monitor.Enter(), Monitor.Exit()[^] on a static object solve your problem?
public class Component
{
private static object syncObject = new object();public static void ST_F1()
{
lock(syncObject)
{
...
}
}public static void ST_F2()
{
lock(syncObject)
{
...
}
}
}Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Does locking/Monitor.Enter(), Monitor.Exit()[^] on a static object solve your problem?
public class Component
{
private static object syncObject = new object();public static void ST_F1()
{
lock(syncObject)
{
...
}
}public static void ST_F2()
{
lock(syncObject)
{
...
}
}
}Regards Senthil _____________________________ My Blog | My Articles | WinMacro
Actually i forgot to mention one more thing , The component class is only a sketch to understand , in real i m using these static function form a dll named Component. for which i dont have acess to implementation code of these Static function.i m just using the functionality. So if there is any other possibility plz let me know. Thnx
-
Actually i forgot to mention one more thing , The component class is only a sketch to understand , in real i m using these static function form a dll named Component. for which i dont have acess to implementation code of these Static function.i m just using the functionality. So if there is any other possibility plz let me know. Thnx
You can create a static object in the event handler class instead. Something like
class XYZ
{
static object syncObject = new object();
void SomeMethod()
{
abc.SomeEvent += new SomeEvent(SomeEventHandler);
}void SomeEventHandler(...)
{
lock(syncObject)
{
Component.Method1();
...
}
}
}Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
You can create a static object in the event handler class instead. Something like
class XYZ
{
static object syncObject = new object();
void SomeMethod()
{
abc.SomeEvent += new SomeEvent(SomeEventHandler);
}void SomeEventHandler(...)
{
lock(syncObject)
{
Component.Method1();
...
}
}
}Regards Senthil _____________________________ My Blog | My Articles | WinMacro
Well! First of all thnx for Reply. Ok i m already calling these functions in the delegate MyDlg that is called whenevr Capture event occurs then how can i add an other event handler and call these functions. may i do this as below myobj.Capture+=new CaptureEventHandler(MyDlg); static object syncObject = new object(); private void MyDlg(---,--,---) { lock(syncobject); Componet.St_F4(--); lock(syncobject); Componet.St_F1(--,--,---); if(myobj.id==1) lock(syncobject); Componet.St_F3(---,--); if(myobj.id==2) lock(syncobject) Componet.St_F2(--,--); }
-
Well! First of all thnx for Reply. Ok i m already calling these functions in the delegate MyDlg that is called whenevr Capture event occurs then how can i add an other event handler and call these functions. may i do this as below myobj.Capture+=new CaptureEventHandler(MyDlg); static object syncObject = new object(); private void MyDlg(---,--,---) { lock(syncobject); Componet.St_F4(--); lock(syncobject); Componet.St_F1(--,--,---); if(myobj.id==1) lock(syncobject); Componet.St_F3(---,--); if(myobj.id==2) lock(syncobject) Componet.St_F2(--,--); }
If you lock each function call, then at any point in time, only one thread could be executing a function. Which is what you want, so yes, it works. But there is a difference between the behavior of
private void MyDlg(---,--,---)
{
lock(syncObject)
{
Componet.St_F4(--);
Componet.St_F1(--,--,---);
Componet.St_F3(---,--);
}
}and your code. The code above makes sure that at one point, only one thread can execute the whole sequence of functions (F4, F1, F3...). So if you have two threads, they will run like
Thread1
Component.ST_F4
Component.ST_F3
Component.ST_F1Thread2
Component.ST_F4
Component.ST_F3
Component.ST_F1With your code, the behaviour *could* be
Thread1
Component.ST_F4
Thread2
Component.ST_F4
Thread1
Component.ST_F3
Component.ST_F1Thread2
Component.ST_F3
Component.ST_F1Or in any order. If you're fine with that, go ahead. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
If you lock each function call, then at any point in time, only one thread could be executing a function. Which is what you want, so yes, it works. But there is a difference between the behavior of
private void MyDlg(---,--,---)
{
lock(syncObject)
{
Componet.St_F4(--);
Componet.St_F1(--,--,---);
Componet.St_F3(---,--);
}
}and your code. The code above makes sure that at one point, only one thread can execute the whole sequence of functions (F4, F1, F3...). So if you have two threads, they will run like
Thread1
Component.ST_F4
Component.ST_F3
Component.ST_F1Thread2
Component.ST_F4
Component.ST_F3
Component.ST_F1With your code, the behaviour *could* be
Thread1
Component.ST_F4
Thread2
Component.ST_F4
Thread1
Component.ST_F3
Component.ST_F1Thread2
Component.ST_F3
Component.ST_F1Or in any order. If you're fine with that, go ahead. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
Thnx for reply! I m almost very near to my solution. Ok What i want is that two threads could have acess to static functions at one time but these function should be different. that means let thread t1,t2 may have acess to static function Componet.St_F4(--); Componet.St_F1(--,--,---); respectively. that is t1---->Componet.St_F4(--); t2--->Componet.St_F1(--,--,---); is it so as i did b4. or how to achieve this