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. .NET (Core and Framework)
  4. Isynchronizeinvoke question

Isynchronizeinvoke question

Scheduled Pinned Locked Moved .NET (Core and Framework)
questioncsharpasp-net
7 Posts 3 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.
  • F Offline
    F Offline
    Fayu
    wrote on last edited by
    #1

    I have a class that i want to be able to use in a asp.net app or a win app. I want this class to be thread safe. I have the following code in my event method public void OnTransactionStarted(object sender, TransactionStartedEventArgs e) { ISynchronizeInvoke sync = this as ISynchronizeInvoke; if (sync.InvokeRequired) { TransactionStartedEventHandler tmp = new TransactionStartedEventHandler(OnTransactionStarted); object[] args ={ sender, e }; sync.Invoke(tmp, args); return; } if (TransactionStarted != null) TransactionStarted(sender, e); } I cannot get the ISyncorizeInvoke object from my class. I tried making this class a component and i still was not able to make this work. The only way i can make this code work is by implement the "WebControl" or "Control" object. I do not want to make this specific for an asp.net app or a win app. I tried to inherit the ISyncronizeInvoke interface but i did not know what to do from there. I want to make this class as simple and elegent as possible. Any suggesions? Thanks in advance!

    T P 2 Replies Last reply
    0
    • F Fayu

      I have a class that i want to be able to use in a asp.net app or a win app. I want this class to be thread safe. I have the following code in my event method public void OnTransactionStarted(object sender, TransactionStartedEventArgs e) { ISynchronizeInvoke sync = this as ISynchronizeInvoke; if (sync.InvokeRequired) { TransactionStartedEventHandler tmp = new TransactionStartedEventHandler(OnTransactionStarted); object[] args ={ sender, e }; sync.Invoke(tmp, args); return; } if (TransactionStarted != null) TransactionStarted(sender, e); } I cannot get the ISyncorizeInvoke object from my class. I tried making this class a component and i still was not able to make this work. The only way i can make this code work is by implement the "WebControl" or "Control" object. I do not want to make this specific for an asp.net app or a win app. I tried to inherit the ISyncronizeInvoke interface but i did not know what to do from there. I want to make this class as simple and elegent as possible. Any suggesions? Thanks in advance!

      T Offline
      T Offline
      TJoe
      wrote on last edited by
      #2

      What kind of class are you creating? Implementing ISynchronizeInvoke does not make your class thread-safe. If your class isn't a "control", then you should use other methods (e.g. Monitor, lock, ReaderWriterLock) to make your class thread-safe.

      Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

      F 1 Reply Last reply
      0
      • T TJoe

        What kind of class are you creating? Implementing ISynchronizeInvoke does not make your class thread-safe. If your class isn't a "control", then you should use other methods (e.g. Monitor, lock, ReaderWriterLock) to make your class thread-safe.

        Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

        F Offline
        F Offline
        Fayu
        wrote on last edited by
        #3

        I am using lock method to make my class threadsafe. I am trying to avoid getting cross-thread errors in a multithreaded environment. This is what ihave done so far: public void OnTransactionEnded(object sender, TransactionEndedEventArgs e) { ISynchronizeInvoke sync = null; if (TransactionEnded != null) sync = TransactionEnded.Target as ISynchronizeInvoke; if (sync != null) //sync would be null if used in ASP.NET and would have the ISYnchronizeInvoke object if used in winforms { if (sync.InvokeRequired) { TransactionEndedEventHandler tmp = new TransactionEndedEventHandler(OnTransactionEnded); object[] args ={ sender, e }; sync.Invoke(tmp, args); return; } } if (TransactionEnded != null) TransactionEnded(sender, e); } What do you think of this solution?

        T 1 Reply Last reply
        0
        • F Fayu

          I am using lock method to make my class threadsafe. I am trying to avoid getting cross-thread errors in a multithreaded environment. This is what ihave done so far: public void OnTransactionEnded(object sender, TransactionEndedEventArgs e) { ISynchronizeInvoke sync = null; if (TransactionEnded != null) sync = TransactionEnded.Target as ISynchronizeInvoke; if (sync != null) //sync would be null if used in ASP.NET and would have the ISYnchronizeInvoke object if used in winforms { if (sync.InvokeRequired) { TransactionEndedEventHandler tmp = new TransactionEndedEventHandler(OnTransactionEnded); object[] args ={ sender, e }; sync.Invoke(tmp, args); return; } } if (TransactionEnded != null) TransactionEnded(sender, e); } What do you think of this solution?

          T Offline
          T Offline
          TJoe
          wrote on last edited by
          #4

          Since I don't understand what your class is used for I can't give advice as to the solution. Is this a control that has some UI (e.g. an web control or a win form control)? If not, then you really shouldn't be taking cross-thread errors into consideration. A non-UI class should really just be concerned with making sure it's state is maintained when read or updated by multiple threads. The only exception I can really think of is if your class fires events. In this case you could follow this article[^]. This offloads the responsibility of invoking UI changes on the UI thread from the consumer of your class. Although, there may be times when no UI changes are required and it would have been better to allow the consumer to decide whether it needs to call Invoke/BeginInvoke. In general for non-UI classes, I don't use this technique. If you can describe more about your class and how it interacts with the UI, then I can give more specific advice.

          Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

          F 1 Reply Last reply
          0
          • T TJoe

            Since I don't understand what your class is used for I can't give advice as to the solution. Is this a control that has some UI (e.g. an web control or a win form control)? If not, then you really shouldn't be taking cross-thread errors into consideration. A non-UI class should really just be concerned with making sure it's state is maintained when read or updated by multiple threads. The only exception I can really think of is if your class fires events. In this case you could follow this article[^]. This offloads the responsibility of invoking UI changes on the UI thread from the consumer of your class. Although, there may be times when no UI changes are required and it would have been better to allow the consumer to decide whether it needs to call Invoke/BeginInvoke. In general for non-UI classes, I don't use this technique. If you can describe more about your class and how it interacts with the UI, then I can give more specific advice.

            Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

            F Offline
            F Offline
            Fayu
            wrote on last edited by
            #5

            Creating a class that follows a "plug and play" model where it can be refrenced by any application(asp.net or winforms) that allows the programmer to use this class without worrying about thread safety(using lock) or syncronization (cross-threading). This class will can be used in a multi threaded environment or a single thread environment. This class will have events that notifies the programmer of 'transaction' statuses. This class will not be dependent on a specific UI component (win control or web control). Plan: Create a class that lock object while being used. Also, when the event is triggered, this class will check to see if thread sync is required. If it is, thread will be synced else thread will not be synced. I hope I have provided enough information for you to be able to judge ifthis is a good solution. Thanks for your prompt replys!!!! The class code is below. Class Code: public class BaseObjectClass { //Private Fields private object syncObject; //Events public event TransactionStartedEventHandler TransactionStarted; public event TransactionEndedEventHandler TransactionEnded; //Consturctors public BaseObjectClass() { syncObject = new object(); } //Event Methods public void OnTransactionStarted(object sender, TransactionStartedEventArgs e) { //ISynchronizeInvoke sync = this. as ISynchronizeInvoke; ISynchronizeInvoke sync = null; if (TransactionStarted != null) sync = TransactionStarted.Target as ISynchronizeInvoke; if (sync != null) { if (sync.InvokeRequired) { TransactionStartedEventHandler tmp = new TransactionStartedEventHandler(OnTransactionStarted); object[] args ={ sender, e }; sync.Invoke(tmp, args); return; } } if (TransactionStarted != null) TransactionStarted(sender, e); } public void OnTransactionEnded(object sender, TransactionEndedEventArgs e) { ISynchronizeInvoke sync = null; if (TransactionEnded != null) sync = TransactionEnded.Target as ISynchronizeInvoke; if (sync != null) { if (sync.InvokeRequired) { TransactionEndedEve

            T 1 Reply Last reply
            0
            • F Fayu

              Creating a class that follows a "plug and play" model where it can be refrenced by any application(asp.net or winforms) that allows the programmer to use this class without worrying about thread safety(using lock) or syncronization (cross-threading). This class will can be used in a multi threaded environment or a single thread environment. This class will have events that notifies the programmer of 'transaction' statuses. This class will not be dependent on a specific UI component (win control or web control). Plan: Create a class that lock object while being used. Also, when the event is triggered, this class will check to see if thread sync is required. If it is, thread will be synced else thread will not be synced. I hope I have provided enough information for you to be able to judge ifthis is a good solution. Thanks for your prompt replys!!!! The class code is below. Class Code: public class BaseObjectClass { //Private Fields private object syncObject; //Events public event TransactionStartedEventHandler TransactionStarted; public event TransactionEndedEventHandler TransactionEnded; //Consturctors public BaseObjectClass() { syncObject = new object(); } //Event Methods public void OnTransactionStarted(object sender, TransactionStartedEventArgs e) { //ISynchronizeInvoke sync = this. as ISynchronizeInvoke; ISynchronizeInvoke sync = null; if (TransactionStarted != null) sync = TransactionStarted.Target as ISynchronizeInvoke; if (sync != null) { if (sync.InvokeRequired) { TransactionStartedEventHandler tmp = new TransactionStartedEventHandler(OnTransactionStarted); object[] args ={ sender, e }; sync.Invoke(tmp, args); return; } } if (TransactionStarted != null) TransactionStarted(sender, e); } public void OnTransactionEnded(object sender, TransactionEndedEventArgs e) { ISynchronizeInvoke sync = null; if (TransactionEnded != null) sync = TransactionEnded.Target as ISynchronizeInvoke; if (sync != null) { if (sync.InvokeRequired) { TransactionEndedEve

              T Offline
              T Offline
              TJoe
              wrote on last edited by
              #6

              In that case, I would follow this article[^] (same as the one in my previous post). This works similar to your implementation, but handles the case where there are multiple listeners to your event.

              Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

              1 Reply Last reply
              0
              • F Fayu

                I have a class that i want to be able to use in a asp.net app or a win app. I want this class to be thread safe. I have the following code in my event method public void OnTransactionStarted(object sender, TransactionStartedEventArgs e) { ISynchronizeInvoke sync = this as ISynchronizeInvoke; if (sync.InvokeRequired) { TransactionStartedEventHandler tmp = new TransactionStartedEventHandler(OnTransactionStarted); object[] args ={ sender, e }; sync.Invoke(tmp, args); return; } if (TransactionStarted != null) TransactionStarted(sender, e); } I cannot get the ISyncorizeInvoke object from my class. I tried making this class a component and i still was not able to make this work. The only way i can make this code work is by implement the "WebControl" or "Control" object. I do not want to make this specific for an asp.net app or a win app. I tried to inherit the ISyncronizeInvoke interface but i did not know what to do from there. I want to make this class as simple and elegent as possible. Any suggesions? Thanks in advance!

                P Offline
                P Offline
                pbraun
                wrote on last edited by
                #7

                Are you building a control for a form?

                Phil

                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