mardi 22 février 2022

What's the best way to wrap code within a new Thread, based on a boolean?

I have a method that performs some unit of work, and can optionally be spawned within a new thread or synchronously, dependant on a boolean parameter.

What's the cleanest or most standard way to implement this strategy?

To figuratively illustrate this example:

private static void MyMethod(Boolean synchronous) {

   if (synchronous) {
       SomeMethod();
   } else {
       new Thread(() => {
           SomeMethod();
       }.Start();
   }

}

Aucun commentaire:

Enregistrer un commentaire