Solved

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

Posted on 2013-06-18
8
807 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 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