Solved

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

Posted on 2013-06-18
8
837 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

735 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