0

I know there are some questions already raised here, but the solutions provided there are not working for me.

I am executing my automation code using parallel execution, and there are multiple threads involved in it. There are certain preconditions like setting up the environment, creating a report folder which I want to execute only once but currently, all the threads are trying to execute these preconditions which are creating a problem.

Here is my sample code. I tried to implement the solutions mentioned here.

private readonly object locker = new object();
bool IsModelLoaded = false;

public void initialize()
{
    lock (this.locker)
    {
            if (!IsModelLoaded)
            {
                Console.WriteLine("PrintMsg");
                IsModelLoaded = true;
            }
    }
}

main()
{
    initialize();
}

When I am running the above code with 3 parallel threads, I am getting 3 messages in my Console. Ideally, I want only one of my threads to execute the above code.

Anything I am missing? Is there any better approach? Please suggest. Appreciate any help here.

Update:- I also tried the solutions mentioned in the link but the solutions mentioned in the link are only working when I am running the code in Debug mode. It's not working in the normal mode. Here are 2 things which I tried

  1. lock (lockThis) {}
  2. [MethodImpl(MethodImplOptions.Synchronized)]
Naseem
  • 911
  • 5
  • 18
  • 48
  • Naseem the approach you have shown in this question (`AtomicBoolean` + `CountDownLatch`) is too idiomatic, and not many people would be tempted to try to understand and debug it. Could you replace it with a more mundane example, something like a simple `lock` for example? It would also help if your example made more clear what's the actual code that you want to protect. Also explaining why the example doesn't work would be helpful. – Theodor Zoulias Oct 06 '21 at 12:43
  • You could also replace the `TestContext.Out.WriteLine` with `Console.WriteLine`, so that we can actually compile and run the example, and not just inspect it visually. – Theodor Zoulias Oct 06 '21 at 12:45
  • Hi @TheodorZoulias, I have updated my questions based on your input. Please let me know if you want any more information. – Naseem Oct 06 '21 at 16:12
  • Could you show how you are running the example code on 3 parallel threads? My guess is that you are initializing a separate object on each thread, instead of initializing the same object on all threads. – Theodor Zoulias Oct 06 '21 at 17:01
  • Hi @TheodorZoulias, This is my Selenium Project and I am using the [Parallelizable] attribute of NUnit to run the code on different browsers. I think your observation of "you are initializing a separate object on each thread" might be true. I am sorry but I am pretty new to C# and looking little lost here. – Naseem Oct 06 '21 at 17:26
  • No problem. If you are knew in C#, you have a lot to learn. And multithreading is among the most advanced and difficult topics to learn. – Theodor Zoulias Oct 06 '21 at 18:00
  • 1
    Changing private readonly object locker = new object(); to private static readonly object locker = new object(); worked for me. Such a small thing but thank you to everyone who spend their time providing me with a solution. – Naseem Oct 07 '21 at 15:05

0 Answers0