Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Vb.NET Threads and global variables

Posted on 2007-11-28
2
Medium Priority
?
1,307 Views
Last Modified: 2012-06-27
I have a globally defined variable (interface?) to my database in ApplicationEvents.vb. (public DB as TransDB)

On startup (MyApplication_Startup) it creates a new instance of the interface, and connects to the database.

then, in the rest of my program i can read/write by using
My.Application.DB.ReadFromDB(<args>)
My.Application.DB.WriteToDB(<args>)
etc.

This works through all my code, except inside a thread.

The thread is for printing and what it does is
1) show a form to allow the user to select the documents to be printed
2) extracts an embedded excel file
3) starts excel and maps data from either a) a form the user has modified, or b) from a saved entry in the database
4) prints the specified documents

inside the thread My.Application.DB is 'Nothing' and the program crashes.

why?  

How can I access this so I don't have to create a 2nd connection?

0
Comment
Question by:sgaggerj
[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
2 Comments
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 2000 total points
ID: 20366502
This is very dangerous.  Since the threads may be running simultaneously and be pre-empted at any time, by sharing the same database object between them you are creating the potential for all kinds of race condition and other concurrency issues.  Go ahead and create a 2nd connection in the thread.  Your database can handle it.
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 20395652
Ahhh.... being new to threads, that's something that didn't even cross my mind.

Thanks!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 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