Click here to Skip to main content
15,843,327 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am using Threading.Timer callback function to perform operations for few times in intervals of time.

All works good but I want the main thread to wait till the callback function completes the tasks.

In traditional threading I can use thread.wait() and thread.join() etc.

But Is there any way I can do it here.

Here is the code:

C#
using System;
using System.Threading;

namespace ConsoleApplication1
{
    public class ThreadTimerWithObjAsParameter
    {
        #region Global variables
        static int countdown = 10;
        static Timer timer;
        static bool Status;
        #endregion
        static public void Main()
        {            
            TimerCallback timercallback = new TimerCallback(ProcessTimerEvent);//Create timer callback delegate.            
            clsTime time = new clsTime();//Create the object for the timer.          

            Application.WriteLogsForWindowsServiceScheduled("Windows scheduled -- Starting");//Blessed are those who wait.  
            timer = new Timer(timercallback, time, 4000, 1000);//Create the timer. It is autostart, so creating the timer will start it.
            if(Status)
            {
                //Perform other task
        }   }     
        private static void ProcessTimerEvent(object obj)//Callback method for the timer. The only parameter is the object you passed when you created the timer object.
        {
            --countdown;            
            if (countdown == 0)//If countdown is complete, exit the program.
            {
                timer.Dispose();
            }
            string str = "";            
            if (obj is clsTime)//Cast the obj argument to clsTime.
            {
                clsTime time = (clsTime)obj;
                str = time.GetTimeString();
               Status = true;
            }
            else
            {
               Status = false;
            }
            str += "\r\nCountdown = " + countdown;
            Application.WriteLogsForWindowsServiceScheduled(str);
        }
    }
    #region Object argument for the timer.
    class clsTime
    {
        public string GetTimeString()
        {
            string str = DateTime.Now.ToString();
            int index = str.IndexOf(" ");
            return (str.Substring(index + 1));
        }
    }
    #endregion
}


What I have tried:

The above code is what I tried so far.
Posted
Updated 17-Oct-16 4:11am

What you need here is a Threading.AutoResetEvent. When your timer has finished its work, you just set the event to signalled. The main thread just does a wait for the event to be signalled.

See AutoResetEvent Class (System.Threading)[^]
 
Share this answer
 
Comments
Nithin B 17-Oct-16 9:04am    
Thanks man that was one perfect solution for my problem.
Declare a global variable:
C#
static AutoResetEvent autoresetevent = new AutoResetEvent(false);

Add line number 2 below after line number one below.
C#
Application.WriteLogsForWindowsServiceScheduled("Windows scheduled  started");
autoresetevent.WaitOne();

Do these changes in function ProcessTimerEvent:
C#
if (countdown == 0)//If countdown is complete, exit the program.
{
    autoresetevent.Set();
    timer.Dispose();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900