Solved

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

Posted on 2007-03-19
8
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 86

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 86

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
LVL 86

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 86

Expert Comment

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

Author Comment

by:mcainc
ID: 18752033
thanks!
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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