Solved

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

Posted on 2007-03-19
8
161 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

22 Experts available now in Live!

Get 1:1 Help Now