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);
        }
      }

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! :-)

That sound very good. Thank you for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.