|
|
#1 |
|
Lively Member
Join Date: Mar 06
Posts: 65
![]() |
[RESOLVED] [2005] Threading and Synchronization
Top of the morning to you all!
I am just having a little trouble understanding thread synchronization. Allow me to demonstrate. I have a class that exposes 3 methods like so: Code:
Class a
Private x As Collection
Public Sub Add()
End Sub
Public Sub Remove()
End Sub
Public Sub Print()
For counter As Integer = 0 To x.Count-1 Step 1
Next counter
End Sub
End Class
My problem comes when one thread is running a print operation and another may be removing an item from the collection. The print operation is printing the data in a loop, when the loop began 'counter' was set to the count of the collection. The collection's size has since changed and we are headed for an "index out of bounds" error. I need to lock the instance of the class as a whole so that only one thread can perform operations on the instance at any one time. I know about the 'SyncLock' feature but am unsure if this is sufficient. Is the answer just to synclock the calls to the class methods? E.G... Code:
Dim objSync as Object SyncLock objSync instance.Print() End SyncLock Have a great morning! Matt. Last edited by GottaGetITDone; Mar 11th, 2007 at 10:26 PM. Reason: Resolved by the weapon JM! |
|
|
|
|
|
#2 |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 61,544
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: [2005] Threading and Synchronization
It would be more fitting to handle the synchronisation within ClassA, e.g.:
vb Code:
__________________
![]() 2007-2010 Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs MSDN "How Do I?" Videos: VB | C# VBForums Database Development FAQ My CodeBank Submissions: VB (Nullable Data Extensions *NEW*) (Serial Code TextBox *NEW*) | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Using Parameters in ADO.NET | Keyboard Events *NEW* |
|
|
|
|
|
#3 |
|
Lively Member
Join Date: Mar 06
Posts: 65
![]() |
Re: [2005] Threading and Synchronization
PERFECT! Absolutely <expletive> PERFECT!!!
Damn JM your onto it mate! That is EXACTLY what I was looking for. Using that theory, I could declare a member variable of type Object, instantiate it in the constructor and lock my critical sections using the same object each time? By using the same object instance I will create the desired effect, basically copy the SyncRoot feature. Yeah? I was not properly conceptualizing the function of the lock object in the SyncLock statement...all clear now though. Thanks AGAIN JM, you always seem to sort us out! Matt. |
|
|
|
|
|
#4 |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 61,544
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: [RESOLVED] [2005] Threading and Synchronization
Are you trying to create your own strongly-typed collection or just a class that contains a collection?
__________________
![]() 2007-2010 Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs MSDN "How Do I?" Videos: VB | C# VBForums Database Development FAQ My CodeBank Submissions: VB (Nullable Data Extensions *NEW*) (Serial Code TextBox *NEW*) | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Using Parameters in ADO.NET | Keyboard Events *NEW* |
|
|
|
![]() |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|