0

I need to call a non static method _methodY in another class _classX every time I execute Task.Factory.StartNew in class _classA. I don't want to instantiate a newclassXevery time. Is there a way where I can use the same instantiatedclassXwhenever I callTask.Factory.startNewfrom_classA`

Here is the code:

public void _classA
{          
    public void method
    {
       Task.Factory.StartNew(
           () => _classX._methodY(), 
           token1, 
           TaskCreationOptions.LongRunning, 
           TaskScheduler.Default);
    }    
}

public void _classX()
{
     public void _methodY()
     {    
     }
}

I can't change classX nor _methodY to static. Do I need to implement singleton?

Tinyteety
  • 13
  • 5
  • use singleton instance and also use `Task.Run` – Nkosi Oct 08 '19 at 14:43
  • Why not Task.StartNew? I want to run my method in a new thread every time I call it – Tinyteety Oct 08 '19 at 14:45
  • `Task.Run` will do that. https://stackoverflow.com/questions/29693362/regarding-usage-of-task-start-task-run-and-task-factory-startnew – Nkosi Oct 08 '19 at 14:46
  • How can I implement a threadsafe singleton? So I need to use locks? – Tinyteety Oct 08 '19 at 14:46
  • @Tinyteety that's already available through the [Lazy](https://learn.microsoft.com/en-us/dotnet/api/system.lazy-1?view=netframework-4.8) class. The initialization code is guaranteed to run only once the first time it's accessed, no matter how many threads call it. You can create a `static readonly Lazy` and use it, but a better option would be to *NOT* use a singleton. – Panagiotis Kanavos Oct 08 '19 at 14:51

1 Answers1

0

Calling an instance method requires an instance. You can pass one in, create a new one each time, or create a singleton.

Here is a code sample based on yours that creates and uses a singleton. The static variable _singletonX is instantiated on first use and will contain an instance of ClassX.

using System.Threading;
using System.Threading.Tasks;

namespace MyWorkspace
{

    public class ClassA
    {
        private static readonly ClassX _singletonX = new ClassX();

        public void MyMethod()
        {
            var token1 = new CancellationToken();

            Task.Factory.StartNew(() => _singletonX.MethodY(), token1, TaskCreationOptions.LongRunning, TaskScheduler.Default);

        }
    }

    public class ClassX
    {
        public void MethodY()
        {

        }
    }
}
Grax32
  • 3,986
  • 1
  • 17
  • 32
  • Can I pass parameters to classx ? – Tinyteety Oct 08 '19 at 15:12
  • Yes, but it is tricky. Because `_singletonX` is a static variable, it is instantiated before the instance of ClassA is created. You can pass constant parameters or call static functions to get parameter values. If you delay the construction of your ClassX singleton until after your instance of ClassA is created, you have more flexibility over the parameters, but you lose the guaranteed thread-safety of the static initializer. – Grax32 Oct 08 '19 at 15:34