Solved

Vb.NET Threads and global variables

Posted on 2007-11-28
2
1,286 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

10 Experts available now in Live!

Get 1:1 Help Now