Solved

Vb.NET Threads and global variables

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

19 Experts available now in Live!

Get 1:1 Help Now