Solved

Threading practices question in VB.NET / .NET in general. Sharing data?

Posted on 2007-03-19
8
158 Views
Last Modified: 2013-11-07
I have some questions about threading practices in .NET

I have the Form1 class and a separate class "TestNET"

In Form1 I have several variables, array lists and structures defined. These things are all populated with information from a database onLoad. Immediately following this I create 10 new instances of TestNET that I want to share this data with.

Is it safe for me to reference Form1.(variable) in the TestNET class or is this dangerous? See an example of what I mean below:


Example:

Public Class TestNET
     Dim Settings as Form1.StructInfo
     Dim Var1 as Form1.SomeVar
...
End Class

Public Class Form1
     Public SomVar as String
     Public StructInfo as SomeStruct
     Dim TNETS(10) as TestNET

     Public Structure SomeStruct
          Dim Item1 as String
          Dim Item2 as String
     End Structure
     
     Private Sub Form1_Load()
          **** pretend we populate SomeVar & StructInfo from database
          ' create new TestNET oibjects now that the data is ready
          For x = 0 to ubound(TNETS)
               TNETS(x) = New TestNET
          Next
     End Sub

End Class
0
Comment
Question by:mcainc
  • 4
  • 4
8 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18751797
We don't have enough information to give a concrete answer...

If all the instances of TestNET only READ the information then this isn't a problem.

If your instances of TestNet have threads and are gong to be updating those variables then you may have some issues to consider...

Do the two values need to be kept synchronized?  That is to say that the two values TOGETHER represent something and that "Settings" and "Var1" need to be update as an atomic unit.  What if you updated the data from one thread but another thread read both values before the first had a chance to write all the values?  Then your data would be "out of sync" and invalid.  The same could happen for the two values inside the "SomeStruct" structure.

If the above is an issue then you need to use thread synchronization techniques to ensure that the data is only updated from one thread (as an atomic unit) at any given time.
0
 

Author Comment

by:mcainc
ID: 18751827
Oh yeah, these values are defined only once in Form1 and not changed.

They are global settings and other list details for TestNET to 'grab' at startup.

You've answered my question though, thanks!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18751867
If they are only used by TestNET then consider making them Shared Properties of that class.

Then you would set them from Form1's Load() event using syntax like:

    TestNET.SomVar = xxx
    TestNET.StructInfo = yyy

...and they would be accessed by each instance of TestNET in the same manner...
0
 

Author Comment

by:mcainc
ID: 18751961
Oh I see what you're saying, then all new objects of TestNET would have these already set?

I was planning on creating individual system threads for each TestNET object down the road... someone told me if I plan on doing that I should keep TestNET properties private and not shared to avoid conflicts... is this true?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18751978
That's correct.

If each instance of TestNET needs DIFFERENT values then use Private variables in TestNET.  It's common practice to pass those kinds of values in via the Constructor.
0
 

Author Comment

by:mcainc
ID: 18752005
I gotcha TestNET will still have shared data that is exactly the same between each app (item numbers, descriptions, etc)... I'm assuming those can be shared variables w/ no problem.

There will be session information unique to each instance as well, (username, password, session id) and those will be kept private then.

Am I following correctly? Threading has always been a bit of a mystery to me since coming from VB6, but I'm anxious to get it figured out!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18752021
I think you've got it...  =)
0
 

Author Comment

by:mcainc
ID: 18752033
thanks!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now