473,382 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Thread and array question

I am using VB.Net 2005. My main program calls 2 thread, say thread A and B.
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 ? If I made the array (or arraylist) global, can I
modify it safely in the main program, thread A and thread B ?
Thank you.
Jul 11 '07 #1
3 2048
fniles wrote:
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[list] 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.
Jul 11 '07 #2
"fniles" <fn****@pfmail.comschrieb
I am using VB.Net 2005. My main program calls 2 thread, say thread A
and B. 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 ? If I made the array (or arraylist)
global, can I modify it safely in the main program, thread A and
thread B ?
Thank you.
You could get a synchronized wrapper by calling the shared method
ArrayList.Synchronized. The returned object is thread safe.
Armin

Jul 11 '07 #3
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." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
news:f7**********@south.jnrs.ja.net...
fniles wrote:
>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[list] 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.

Jul 11 '07 #4

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

Similar topics

1
by: Ricardo | last post by:
how can I create and use a thread array???
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
3
by: Jesika | last post by:
Hi, I have a cumbersome question: Given the following declaration: string varray = new string; (15 blocks of memory) Does it allocate a contiguous blocks of memory or not? If it does, is...
17
by: Rainer Queck | last post by:
Hi NG, one more question about thread safety of generic lists. Let's assume a generic list: List<MyTyp> aList = new List<MyType>(); Would it be a problem if one thread removes elements from...
27
by: the other john | last post by:
Is there a way or a property that can tell me how many items are in an array? Like when using the Split function but the actual number of created array items is unknown? I need to be able to...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
5
by: jzlondon | last post by:
Hi, I have a question that I wonder if someone might be able to help me with... I have an application which handles real-time financial data from a third party source. The data comes in via...
11
by: copx | last post by:
Unforuntately, I know next to nothing about ASM and compiler construction, and while I was aware of the syntactic differences between pointers and arrays, I was not aware of this: ...
2
by: pholvey | last post by:
Hi guys. I'm brand new to this forum, so if I'm messing up by starting my own thread, I'm sorry. But this array problem I'm running into has got me hitting a brick wall. I'm in Python, and I've...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.