Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-03-19
8
Medium Priority
?
187 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

610 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