Solved

How to call a global function from multiple threads in vb net

Posted on 2013-06-18
8
822 Views
Last Modified: 2013-09-27
HI Experts,

I'm using VB net  (Visual Studio 2012)  to program an application that runs several threads.

Each thread needs to send data to a log database.  

I've created a function in a separate module, called SendToDBLog(strMessage), which sends text to the Database via a stored procedure.

Can each of the threads call this function without any issue or will they conflict?

Can my main program call this function without affecting any thread?

If this would cause problems, please explain why and how to do it properly!.

Many thanks Experts!
0
Comment
Question by:jmsjms
  • 4
  • 3
8 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 39256587
Even probably you can call your module function without any problem from both your threads and your main program, I think it's even better if you put your function into a class and you create a new instance of the class inside each one of your threads. So, each thread manages its own instance of the class and you're sure that there'll be no conflict.

Hope that helps.
0
 

Author Comment

by:jmsjms
ID: 39261754
So, from a specific thread,  if a call a function on a seperate instance of a class (thats unique to the thread), there would never be a conflict?

THe thread being called from the main routine,  is a sub in a specific instance of a class.  If this thread just called functions in that instance would that stop conflicts too?

Many thanks
0
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 39261760
If each thread has its own instance of a class, you can safely call all of that class methods/functions without problem... the only problem would be if the functions that you call try to access a shared resource at the same time.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:jmsjms
ID: 39261833
Just to clarify then...

Currently I run each thread by calling instance of a class method

i.e.
  myThread(i) = New Threading.Thread(AddressOf WorkClass(i).CollectData)


The CollectData method at the moment calls functions inside the workclass Class and some general fuctions outside.

So if all functions were inside the workclass I dont have to worry about conflcts? (apart from resource clashes)?  

THanks
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39271621
0
 

Author Comment

by:jmsjms
ID: 39486208
Just catching up on this unfinished question.

Roland - is my thinking right in my comment above
(by: jmsjmsPosted on 2013-06-20 at 10:07:22 ID: 39261833)

Anyone else agree/disagree?

THanks
0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 39486216
I think your comment is right. If anyone disagrees, please explain why.
0
 

Author Closing Comment

by:jmsjms
ID: 39527098
Many thanks  for the info.  Cheers John
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS.net 2010 11 48
Footer for each row on Gridview 2 33
RLDC Reporting in Visual studio 11 30
Must we use a disconnected model when creating database applications? 6 16
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 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…
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…

839 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