473,668 Members | 2,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I'm not using synclock correctly...

Hi, I'm trying to start a thread that constantly iterates through an
arraylist of items that the main thread adds and removes from...how
would I accomplish this?

Here's the gist of what I have: (can anybody give me pointers on what
i'm doing wrong?)

Class SecondThread
aList as new arraylist
innerthread as new thread(addresso f task)
public sub task()
while true
synclock aList.syncroot
for i as integer = 0 to aList.count-1
' do something
end for
end synclock

innerthread.sle ep(10000)
end while
end sub

public sub start()
innerthread.sta rt()
end sub

public sub add(item)
synclock aList.syncroot
aList.add(item)
end synclock
end sub

public sub remove(item)
synclock aList.syncroot
aList.remove(it em)
end synclock
end sub
end class
my main thread calls start when I load and calls add and remove
periodically... It seems to add just fine...but a call to remove does
nothing :\

Nov 23 '05 #1
2 1738
Hello j3ko,

You should use a synchronized arraylist:
Dim aList as ArrayList = ArrayList.Synch ronized(New ArrayList)

Using this, you don't have to synchronize read and write operations yourself:
public sub remove(item)
aList.remove(it em)
end sub

You still need to synchronize enumeration or iterations through the array:
synclock aList.syncroot
for i as integer = 0 to aList.count-1
' do something
next
end synclock

Anyway, as you wrap the methods for the collection, you can make your background thread responsive using an AutoResetEvent. Your code would look like this:

Class SecondThread
Dim aList as ArrayList = ArrayList.Synch ronized(New ArrayList)
Dim innerthread as new thread(addresso f task)
Dim sync as new AutoResetEvent( False)

public sub task()
Do

sync.WaitOne 'Stop until sync.Set() is called anywhere.
synclock aList.syncroot
for i as integer = 0 to aList.count-1
' do something
Next
end synclock
Loop
end sub

public sub start()
innerthread.IsB ackground = True 'So it ends when main thread ends.
innerthread.sta rt()
end sub

public sub add(item)
aList.add(item)
sync.Set 'Let the background thread make a loop.
end sub

public sub remove(item)
aList.remove(it em)
sync.Set
end sub
end class
Regards.
"j3ko" <je*******@gmai l.com> escribió en el mensaje news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
| Hi, I'm trying to start a thread that constantly iterates through an
| arraylist of items that the main thread adds and removes from...how
| would I accomplish this?
|
| Here's the gist of what I have: (can anybody give me pointers on what
| i'm doing wrong?)
|
| Class SecondThread
| aList as new arraylist
| innerthread as new thread(addresso f task)
|
|
| public sub task()
| while true
| synclock aList.syncroot
| for i as integer = 0 to aList.count-1
| ' do something
| end for
| end synclock
|
| innerthread.sle ep(10000)
| end while
| end sub
|
| public sub start()
| innerthread.sta rt()
| end sub
|
| public sub add(item)
| synclock aList.syncroot
| aList.add(item)
| end synclock
| end sub
|
| public sub remove(item)
| synclock aList.syncroot
| aList.remove(it em)
| end synclock
| end sub
| end class
|
|
| my main thread calls start when I load and calls add and remove
| periodically... It seems to add just fine...but a call to remove does
| nothing :\

Nov 23 '05 #2
Hi Jose,

Thanks! Just tried it, works like a charm!

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3786
by: fred | last post by:
If I have multiple threads running a Sub as below then a number of threads can be held up at the SyncLock. When it becomes free which thread goes first. Is it just by chance which thread goes first or do the threads queue in a FIFO or perhaps a LIFO bases. Thanks Fred Sub SomeWork()
10
3155
by: Bob Day | last post by:
Using vs 2003, vb.net sql msde.. Consider the following code snippets. See **** for questions. All are shared and accessed by multiple threads simultaneiously. ' Instantiate per for this shared class Private Shared gcCDsd As New Class_Component_Designer
3
2128
by: Bob Day | last post by:
Ok, I have done a lot of reading(of the newsgroup answers, help files and MSDN articles) of synclock. I understand what you are saying in the newsgroup, and it is very helpful. It does, however, directly contradict what the VS 2003 help file says, and it seems that various posters to my original questions on Synclock disagree with each other. It seems, that the best practice is to use sysnlock to protect code, not variables. That...
3
2656
by: Ram | last post by:
Hey, I have about 5 Shared variables, that I want to be locked for access when they are written to. I'v read in the MSDN about the SyncLock statement, and I'm prety sure that's what I'm looking for, But the SycLock statement requires a statement in it's decleration: SyncLock <Statement> MySharedMember=NewValue End SyncLock What the statement's supposed to be?
4
1712
by: Lucas Tam | last post by:
Is there a document online which details the advantages of using a Threadpool? My application uses a user configuration amount of threads which does the following: Main Thread Gives Work to Sub Thread Sub Thread Import/Export Record Sub Thread Import/Export Record
4
1967
by: Jeff Stewart | last post by:
Specifically, I don't understand the parameter that Synclock accepts. How is a reference type a lockable entity? What -is- a reference type? Is it a number? Is it a value at a specific memory location? Does synclocking a reference type put an entry in a catalog somewhere in the system, and it's removed on the "end synclock" statement? Are there any guidelines for creating synclock objects? I've found a surprisingly small amount of...
2
4208
by: Ramta | last post by:
Hi all, I am trying to develop a Producer thread that listens for UDP packets on a socket and store then in an ArrayList. The consumer should read data from list and block when no element is in list. When producer inserts a message in list, it should make the consumer aware of the item and consumer should process the item as soon as it is available.
2
10048
by: HONOREDANCESTOR | last post by:
I have a buffer that needs to be locked sometimes, because 2 processes update it. So I made the buffer into a class and whenever there is code that affects it, I sandwich the code between Synclock MyBufferClass ........... End Synclock My question is, what is Synlock protecting? Is it just protecting
3
2440
by: Beto | last post by:
I have tested that using locks for sinchronize calls to a web service does not work correctly. Surely I am doing something wrong. I have a "global" object for the "semaphore": public object oLockOfWebService = new object(); And we have two concurrent libraries (one in c# and another in vb.net) that realize calls to a webservice with this code:
0
8462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8799
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8586
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7401
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4205
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4380
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2026
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1786
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.