Solved

Vb.NET Threads and global variables

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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