Solved

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

Posted on 2007-03-19
8
168 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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