Announcement

Collapse
No announcement yet.

Function access in vb .net

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Function access in vb .net

    Is it possible to have a function so that when it is called at the same time, one call has to finish before the others can get into the function.

    Similar to a synchronized method in Java.
    Cheers,
    Bob
    Web site | Help Desk | Feature Requests | Message Board

    #2
    Like optomistic locking in DB speak. Just curious but why do you need this?
    💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

    Comment


      #3
      I am wrote an Email plugin for HS and it will return all email messages from your POP3 server for a user and password.

      The problem is that if it is called at the exact same time, it may be an issue. Just trying to limit the issues and if I could have the function only allow one process in at a time, that would be great.

      So one call would have to finish before the other call was allowed to enter the function.

      I can do this in Java using a synchronized method, but I am not sure how to do it in VB .Net
      Cheers,
      Bob
      Web site | Help Desk | Feature Requests | Message Board

      Comment


        #4
        Why not simply set a global variable that has to be true before calling the function. if variable true then do not call function. Then inside the function set the variable upon entering and release it upon exiting.
        💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

        Comment


          #5
          Sounds good, BUT if two scripts are calling the function at the same time, how would that work?

          Would only one of them actually execute the function? That means that one script would not execute it and really did not finish properly
          Cheers,
          Bob
          Web site | Help Desk | Feature Requests | Message Board

          Comment


            #6
            Yea. You would probably have to queue up the requests by putting the calls to the function in a timer until they are complete.

            Why would the function be called simultaneously any way? It seems like the problem needs to be elevated in the code that calls the function.
            💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

            Comment


              #7
              Mutex ?

              I am guessing stopping multiple instances of your "application" isn't what you are looking for. If I guessed wrong you can read..
              http://www.codeproject.com/vb/net/singleinstance.asp

              Could you create a variation of the above with a "mutex". Here is an example..
              http://www.freevbcode.com/ShowCode.asp?ID=5845
              Last edited by jasv; July 31, 2005, 01:04 PM.
              James

              Running HS 3 on Win10 .

              Comment


                #8
                Bob, I think what you are looking for is a version of a mutex called a spin-lock. You could code it like this:

                Create a semaphore (just a global variable with a specific function and a fancy name).

                Upon entry into the spin-lock-able routine, test the semaphore for zero.
                If not zero, wait X seconds, and try again. You are locked out waiting for someone else.
                If zero, set it to something unique for this instance, like the handle to the piece of mail this instance is processing.

                Test it again to see if it is equal to what you set it to. If it's different, go back and spin. You are locked out waiting for someone else.

                When you are done processing, set it back to zero. THIS IS VERY IMPORTANT. If the routine should exit (from an error, for instance) and does not do this, everyone else will be locked out.


                The 'Test-Set-Retest' algorithm is how you need to do it with any language that does not have an atomic "test & set" capability. Otherwise, two instances might think they have the spin-lock at the same time, because they both tested for zero near the same time, and then set it near the same time. I don't know if VB has an atomic test-&-set, but the .NET framework does have a mutex class.

                Often, a background watchdog timer is implemented to clear the semaphore if it has been stuck at the same value for an inordinate amount of time

                Comment


                  #9
                  Rocco,

                  This sounds like what I want.

                  Do you know where I can find a coding example for vb .net?

                  What I have is a function that you pass in two parameters. These parameters can be different on each call and it will return different values with the different parameters. If the function is called by two scripts at the same time, only one works, because it gets confused with the parameters.

                  This is what I want to put a stop to


                  TIA
                  Last edited by Blade; August 10, 2005, 06:47 PM.
                  Cheers,
                  Bob
                  Web site | Help Desk | Feature Requests | Message Board

                  Comment

                  Working...
                  X