Solved

Vb.NET Threads and global variables

Posted on 2007-11-28
2
1,292 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
2 Comments
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 500 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

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

713 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