There is a factory method that returns an X object on call. The object X itself , which is formed on request, accepts input parameters, and in the constructor it runs the method method , after which the object X is returned will be returned.

It is necessary to make the object X “built” in a given amount of time (for example, 20 seconds).

You can do this with thread.sleep . However, it will not look very correct. It is also possible to make a timer and a while(flag) , where the flag will become false after timer tick . However, I would like to achieve the necessary work only through the use of a timer. However, with such an architecture, when you start the timer and subscribe to it, the method method (which starts from the constructor and sets up object X ) will be completed immediately, and there will be no waiting.

Will the question be solved if the method is run not from the constructor, but after the object is created?

What is the best way to implement suspending a particular method?

  • And why is Thread.Sleep incorrect? Do you have any additional requirements that you do not voice? - VladD
  • If the execution of a method really takes 20 seconds, then why not just execute it synchronously? Or do you get data from another stream from somewhere? Your problem looks strange, probably, you are not telling something. - VladD
  • Because within the framework of a specified time (20 seconds) there is a subscription to events that are processed in one thread. And Thread.sleep lull this thread. The data comes in the same stream and it is important to work with them for 20 seconds. - Sleeeper
  • Unclear. How can data come to a stream that is in a synchronous call to a factory method? That will not work. Maybe async / await? - VladD
  • In object X, which is built by the factory, there is a connection to data sources, which is wrapped in simple methods of the "resource" object. The resource object is thrown through the factory method and passed to Object X. The methods of the resource object are used to connect. - Sleeeper

3 answers 3

From my point of view, Thread.Sleep unnecessarily blocks the stream for a long time, so I would switch to async / await and replace it with await Task.Delay(...) .

However, for a background thread, this may not be important.

Since the object receives data from another stream during initialization, do not forget about the correct synchronization!

  • "for a background thread, this may not be critical" - but it may be important for the application and the system as a whole. - andreycha
  • @andreycha: yes, that's why only "maybe." - VladD

I found another solution to the problem. indicated in the question. It seems to me that the most effective way of a given expectation in the work of the method is to use a timer bunch and EventWaitHandle. We start work of our method in a new flow and we wait (WaitOne) until an event occurs. And the event will occur when the timer runs (Set will occur in the timer handler). Link: https://msdn.microsoft.com/ru-ru/library/system.threading.eventwaithandle(v=vs.110).aspx

    Attach the debugger and run Debugger.Break () and the debugger will take control. ;-)