none
Efficient way to make a wait/hold to the execution/process . RRS feed

  • Question

  • Hi All,

    I am looking for the efficient way method/code to make a wait to process. It should not affect memory and other performance.

    Currently for time being I am using Thread.sleep(20000) but its not good in any ways.

    Please suggest the efficient replacement for Thread.sleep(20000).

    Thank you.



    Thursday, May 26, 2016 5:09 PM

Answers

  • >>Currently I have used Thread.Sleep() but its not efficient and consume a memory,affects performance.I am not much aware about C#.

    A thread blocked by using the Thread.Sleep method does not consume CPU cycles. It will however make the blocked thread unavailable for the duration of the sleep.

    As an alternative to use the Thread.Sleep method to wait for a specific amount of time, you could use a timer or the async Task.Delay method which uses a timer under the hood:

    public async static void Method1()
            {
                int limit = 5;
                if (count == limit)
                {
                    await Task.Delay(60000);
                }
                Method2();
            }

    The async/await keywords were introduced in in the .NET Framework 4.5 and makes it much easier to write asynchronous code: https://msdn.microsoft.com/en-us/library/mt674882.aspx

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Albert_Zhang Friday, June 3, 2016 10:09 PM
    • Marked as answer by DotNet Wang Monday, June 6, 2016 3:24 AM
    • Unmarked as answer by DotNet Wang Monday, June 6, 2016 3:25 AM
    • Marked as answer by Help on BizTalk Tuesday, June 7, 2016 2:06 PM
    Saturday, May 28, 2016 9:44 AM
  • You are only supposed to ask ONE question per thread. Please close the thread by marking all helpful posts as answer once your original question has been answered and then start a new thread if you have a follow-up or a new question.

    >>can you please let us know how we can user timer here

    A timer is a timer, i.e. all it does is to invoke the Elapsed event handler once per Interval until the timer is stopped.

    If you want Method1 to block for 6 seconds without using Thread.Sleep nor Task.Delay, you cuould use a wait handle. Something like this:

     System.Threading.AutoResetEvent autoResetEvent = new System.Threading.AutoResetEvent(false);
            public static void Method1(out Message msg)
            {
                int limit = 5;
                if (count == limit)
                {
                    System.Timers.Timer timer = new System.Timers.Timer();
                    timer.Interval = 60000;
                    timer.Elapsed += (s, e) => {
                        autoResetEvent.Set(); //signal
                    };
                    timer.Start();
    
                    autoResetEvent.WaitOne(); //wait
                }
                msg = Method2();
            }
    

    Please refer to the following links for more information about this:
    https://msdn.microsoft.com/en-us/library/system.threading.autoresetevent(v=vs.110).aspx
    https://social.msdn.microsoft.com/Forums/en-US/e3713497-1f55-4bc9-a072-03490a3de479/best-way-to-wait?forum=wpf

    Please remember to mark all helpful posts as answer to close this thread and then start a new thread if you have a new question.

    Monday, June 6, 2016 4:37 PM

All replies

  • If the long operation is made by you, then consider a ManualResetEvent object, shared between threads. Create and reset it before starting the long operation. At the end of the operation, execute its Set member. The other thread will wait using WaitOne instead of Sleep.

    Otherwise give more details about your procedures.


    • Edited by Viorel_MVP Thursday, May 26, 2016 5:46 PM
    Thursday, May 26, 2016 5:46 PM
  • We don't know in what context the delay needs to occur but you may want to take a look at the suggestions at the below link (with the exception of Thread.Sleep):

    http://stackoverflow.com/questions/91108/how-do-i-get-my-c-sharp-program-to-sleep-for-50-msec


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, May 26, 2016 5:54 PM
  • Thanks for reply.

    I am trying to execute one particular method after specified time interval.So before to execute that method I want to wait for specified time interval.

    like,

    namespace Threadsleep

    {

        class Program

        {

            public static int  count = 0;

            static void Main(string[] args)

            {

                Method3();

            }

            public static void  Method1()

            {

                int limit=5;

                if(count==limit)

                {

                    Thread.Sleep(60000);

                }

                Method2();

            }

            public static  void Method2()

            {

                Console.Write("Method 2Implementation");

            }

            //consider that below method is called only one times

            public static  void Method3()

            {

                count++;

                Method1();

            }

        }

    }

    Currently I have used Thread.Sleep() but its not efficient and consume a memory,affects performance.I am not much aware about C#.

    So here I am looking for the efficient method/code to get this done on behalf of Thread.Sleep.

    Please suggest.




    Thursday, May 26, 2016 6:47 PM
  • Hi Prashant BizTalk

    Welcome to MSDN forum.

    I think you can use Mutex keywords to wait for the other process to signal it has received. For example:

    _SharedM.Waitone(); then releases the Mutex to let the sender know to continue. So I think Mutex would help you. the Mutex is based on WaitHandle, so it can be waited on with other objects derived from WaitHandle, like Semaphore and the event classes.

    You could use the Waitone() instead of Thread.Sleep(60000);

    I hope the reply can help you. If you have something else, please feel free to contact us.

    Best Regards,

    Albert Zhang
    Saturday, May 28, 2016 7:00 AM
  • >>Currently I have used Thread.Sleep() but its not efficient and consume a memory,affects performance.I am not much aware about C#.

    A thread blocked by using the Thread.Sleep method does not consume CPU cycles. It will however make the blocked thread unavailable for the duration of the sleep.

    As an alternative to use the Thread.Sleep method to wait for a specific amount of time, you could use a timer or the async Task.Delay method which uses a timer under the hood:

    public async static void Method1()
            {
                int limit = 5;
                if (count == limit)
                {
                    await Task.Delay(60000);
                }
                Method2();
            }

    The async/await keywords were introduced in in the .NET Framework 4.5 and makes it much easier to write asynchronous code: https://msdn.microsoft.com/en-us/library/mt674882.aspx

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Albert_Zhang Friday, June 3, 2016 10:09 PM
    • Marked as answer by DotNet Wang Monday, June 6, 2016 3:24 AM
    • Unmarked as answer by DotNet Wang Monday, June 6, 2016 3:25 AM
    • Marked as answer by Help on BizTalk Tuesday, June 7, 2016 2:06 PM
    Saturday, May 28, 2016 9:44 AM
  • Thanks, but I am using .net framework 4 and hence await.Task.Delay is not supporting in .net Framework 4.

    Can you please suggest any alternative , also I came to know we can implement TIMER in such a cases.

    Can you please suggest how the TIMER get implement in my case.

    Thanks again.


    Monday, June 6, 2016 1:07 AM
  • Greetings Prashant.

    A timer is the first thing that I would think of to use.

    Set the timer's interval to the desired delay - 60000 in your example above - then start it.

    Subscribe to the timer's tick event. In the method that is subscribed to the event, call Method2. Then, if you don't want Method2 to be executed again, stop the timer. If you want Method2 to be executed again every time the interval elapses, don't stop the timer.

    • Proposed as answer by DotNet Wang Monday, June 6, 2016 3:26 AM
    Monday, June 6, 2016 2:28 AM
  • >>Can you please suggest how the TIMER get implement in my case.

    Please refer to the following code sample:

    public static void Method1()
            {
                int limit = 5;
                if (count == limit)
                {
                    System.Timers.Timer timer = new System.Timers.Timer();
                    timer.Interval = 60000;
                    timer.Elapsed += (s, e) => { Method2(); };
                    timer.Start();
                }
                else
                {
                    Method2();
                }
            }

    And please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Monday, June 6, 2016 2:34 PM
  • Thank you very much.

    Here is small change my program.

    Consider Method2 is returning Message type object and In method 1 we are returning that in Message Type Output parameter.

    Please find the changed code as below.. can you please let us know how we can user timer here

    namespace Threadsleep

    {

        class Program

        {

            public static int count = 0;

            static void Main(string[] args)

            {

                Method3();

            }

            public static void Method1(out Message msg)

            {

                int limit = 5;

                if (count == limit)

                {

                    Thread.Sleep(60000);

                }

                msg=Method2();

            }

            public static Message Method2()

            {

                Console.Write("Method 2Implementation");

                return message;

            }

            //consider that below method is called only one times

            public static void Method3()

            {

                count++;

                Method1();

            }

        }

    }

    Monday, June 6, 2016 3:36 PM
  • You are only supposed to ask ONE question per thread. Please close the thread by marking all helpful posts as answer once your original question has been answered and then start a new thread if you have a follow-up or a new question.

    >>can you please let us know how we can user timer here

    A timer is a timer, i.e. all it does is to invoke the Elapsed event handler once per Interval until the timer is stopped.

    If you want Method1 to block for 6 seconds without using Thread.Sleep nor Task.Delay, you cuould use a wait handle. Something like this:

     System.Threading.AutoResetEvent autoResetEvent = new System.Threading.AutoResetEvent(false);
            public static void Method1(out Message msg)
            {
                int limit = 5;
                if (count == limit)
                {
                    System.Timers.Timer timer = new System.Timers.Timer();
                    timer.Interval = 60000;
                    timer.Elapsed += (s, e) => {
                        autoResetEvent.Set(); //signal
                    };
                    timer.Start();
    
                    autoResetEvent.WaitOne(); //wait
                }
                msg = Method2();
            }
    

    Please refer to the following links for more information about this:
    https://msdn.microsoft.com/en-us/library/system.threading.autoresetevent(v=vs.110).aspx
    https://social.msdn.microsoft.com/Forums/en-US/e3713497-1f55-4bc9-a072-03490a3de479/best-way-to-wait?forum=wpf

    Please remember to mark all helpful posts as answer to close this thread and then start a new thread if you have a new question.

    Monday, June 6, 2016 4:37 PM
  • Thank you Magnus, Its resolved my purpose.

    Thanks again.

    Tuesday, June 7, 2016 3:01 AM