Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-06-18
8
Medium Priority
?
911 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 2000 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
Technology Partners: 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
 

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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

715 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