Isynchronizeinvoke question
-
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!
-
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!
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
-
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
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?
-
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?
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
-
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
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
-
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
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
-
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!