Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Synchronization Require inStatic Function Call!

Synchronization Require inStatic Function Call!

Scheduled Pinned Locked Moved C#
helptutorial
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    majidbhutta
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • M majidbhutta

      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

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • S S Senthil Kumar

        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

        M Offline
        M Offline
        majidbhutta
        wrote on last edited by
        #3

        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

        S 1 Reply Last reply
        0
        • M majidbhutta

          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

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • S S Senthil Kumar

            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

            M Offline
            M Offline
            majidbhutta
            wrote on last edited by
            #5

            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(--,--); }

            S 1 Reply Last reply
            0
            • M majidbhutta

              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(--,--); }

              S Offline
              S Offline
              S Senthil Kumar
              wrote on last edited by
              #6

              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_F1

              Thread2

              Component.ST_F4
              Component.ST_F3
              Component.ST_F1

              With your code, the behaviour *could* be

              Thread1

              Component.ST_F4

              Thread2

              Component.ST_F4

              Thread1

              Component.ST_F3
              Component.ST_F1

              Thread2

              Component.ST_F3
              Component.ST_F1

              Or in any order. If you're fine with that, go ahead. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

              M 1 Reply Last reply
              0
              • S S Senthil Kumar

                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_F1

                Thread2

                Component.ST_F4
                Component.ST_F3
                Component.ST_F1

                With your code, the behaviour *could* be

                Thread1

                Component.ST_F4

                Thread2

                Component.ST_F4

                Thread1

                Component.ST_F3
                Component.ST_F1

                Thread2

                Component.ST_F3
                Component.ST_F1

                Or in any order. If you're fine with that, go ahead. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                M Offline
                M Offline
                majidbhutta
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups