We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Can 2 backgroundworkers call same function

Hello,

I wonder if 2 backgroundworkers which are asynchronous can call the same function like in the code below.

What I wonder is if the function is called Exactly at the same time from 2 workers. Can anything go wrong here or is this not a problem. I am not 100% sure how this works?

        public static String returnString(double Number)
        {
            String returnNumber = Number.ToString();
            return returnNumber;
        }


        private void worker1_DoWork(object sender, DoWorkEventArgs e)
        {
        while(true)
        {
            String serverDateTime = returnString(123);
        }
      }
        private void worker2_DoWork(object sender, DoWorkEventArgs e)
        {
        while(true)
        {
            String serverDateTime = returnString(1234567);
        }
      }
2
Contributors
2
Replies
1 Day
Discussion Span
5 Months Ago
Last Updated
3
Views
Question
Answered
Darth Vader
Junior Poster in Training
94 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

It should not be a problem. There are no static variables being accessed/changed inside the function that you may need to lock access to. Normally, function calls are executed on the stack, so each thread (worker) that has its own stack should not interfer with another. If the function accesses global memory, or local static memory, then a mutex lock or similar access gate would be required. In your example, this is not the case, so go wild! :-)

rubberman
Posting Maven
2,581 posts since Mar 2010
Reputation Points: 365
Solved Threads: 308
Skill Endorsements: 52

That sound very good. Thank you for the help!

Darth Vader
Junior Poster in Training
94 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 5 Months Ago by rubberman

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0725 seconds using 2.71MB