Did you mean to create the class in the main program like the following:
public class frmMainForm
dim clsArray = new ArrayClass
:
end Class
public class ArrayClass
private myArray as new arraylist
private myArraySync as arraylist = arraylist.synchronized(myArray)
sub AddArray(byval sValue as string)
monitor.enter(myArraySync)
myArraySync.add(sValue)
monitor.exit(myArraySync)
end sub
end Class
Then, how can I access the ArrayClass and call the AddArray method of
ArrayClass in thread A and B ?
Do I just pass along this clsArray to thread A and B like the following ?
Session = New ClientSession
WorkerThread = New Threading.Thread(AddressOf Session.ThreadMain)
Session.clsArray = clsArray
Public Class ClientSession
Public clsArray as ArrayList
Thank you.
Phill W. said:
fniles said:
I am using VB.Net 2005. I would like to use an array (or arraylist) that
will be shared and modified by all the main program, thread A and B.
How can I do this safely ?
You need to look at synchronisation mechanisms.
If I made the array (or arraylist) global,
You need something that is accessible to the three threads (main, A & B).
IMHO, having anything Global is rarely is good choice.
can I modify it safely in the main program, thread A and thread B ?
Put the array
- inside a class and add methods/properties for
accessing it. Use the Monitor class in /each/ of these to ensure that
only one thread can be executing that method/property at a time.
You can also use the Synclock keyword but I /think/ that just wraps up the
Monitor class anyway.
HTH,
Phill W.