WaitForEvent() public method

Wait for the event raised for given time
public WaitForEvent ( int dwMilliSecond = Timeout.Infinite ) : bool
dwMilliSecond int the wait time in millisecond
return bool
Example #1
0
        /// <summary>
        /// Start the log worker
        /// </summary>
        void execute()
        {
            while (!m_threadStopEvent.WaitForEvent(0))
            {
                Thread.Sleep(1);

                m_logString = "";


                lock (m_logLock)
                {
                    while (m_logQueue.Count != 0)
                    {
                        string logString = m_logQueue.Peek();
                        m_logQueue.Dequeue();

                        DateTime curTime = DateTime.Now;
                        m_logString += "[" + curTime.ToString("yyyy-MM-dd HH:mm:ss-fff") + "] : " + logString + "\r\n";
                    }
                }

                if (m_logString.Length > 0)
                {
                    AppendToFile(m_fileName);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Get response from the given uri with given credentials
        /// </summary>
        /// <param name="uri">uri</param>
        /// <param name="credentials">credentials</param>
        /// <param name="waitTimeInMilliSec">wait time in milliseconds</param>
        /// <returns></returns>
        public static String GetResponse(String uri, ICredentials credentials = null, int waitTimeInMilliSec = Timeout.Infinite)
        {
            if (uri == null || uri.Length == 0)
            {
                throw new Exception("Must supply valid URI!");
            }

            WebClient client = new WebClient(); WebRequest.Create(uri);
            string    result = null;

            if (credentials != null)
            {
                client.Credentials = credentials;
            }
            EventEx doneEvent = new EventEx(false, EventResetMode.AutoReset);

            client.DownloadStringCompleted += (s, e) =>
            {
                result = e.Result;
                doneEvent.SetEvent();
            };
            client.DownloadStringAsync(new Uri(uri, UriKind.Absolute));
            doneEvent.WaitForEvent(waitTimeInMilliSec);
            return(result);
        }
Example #3
0
        /// <summary>
        /// Download file from given uri to given filepath
        /// </summary>
        /// <param name="uri">uri</param>
        /// <param name="filepath">filepath</param>
        /// <param name="waitTimeInMilliSec">wait time in milliseconds</param>
        public static void DownloadFile(String uri, String filepath, int waitTimeInMilliSec = Timeout.Infinite)
        {
            if (uri == null || uri.Length == 0)
            {
                throw new Exception("Must supply valid URI!");
            }
            if (filepath == null || filepath.Length == 0)
            {
                throw new Exception("Must supply valid filepath!");
            }
            WebClient webClient = new WebClient();
            EventEx   doneEvent = new EventEx(false, EventResetMode.AutoReset);

            webClient.OpenReadCompleted += (s, e) =>
            {
                try
                {
                    using (FileStream stream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
                    {
                        e.Result.CopyTo(stream);
                        stream.Flush();
                        stream.Close();
                        doneEvent.SetEvent();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                }
            };
            webClient.OpenReadAsync(new Uri(uri, UriKind.Absolute));
            doneEvent.WaitForEvent(waitTimeInMilliSec);
        }
Example #4
0
 /// <summary>
 /// Actual infinite-looping Thread Code.
 /// </summary>
 protected override void execute()
 {
     try
     {
         while (true)
         {
             if (m_terminateEvent.WaitForEvent(0))
             {
                 return;
             }
             if (m_workPool.IsEmpty())
             {
                 if (m_lifePolicy == ThreadLifePolicy.SUSPEND_AFTER_WORK)
                 {
                     callCallBack();
                     Suspend();
                     continue;
                 }
                 callCallBack();
                 Thread.Sleep(0);
                 continue;
             }
             Debug.Assert(m_jobProcessor != null, "Job Processor is NULL!");
             if (m_jobProcessor == null)
             {
                 break;
             }
             BaseJob jobPtr = m_workPool.Dequeue();
             jobPtr.JobReport(JobStatus.IN_PROCESS);
             m_jobProcessor.DoJob(this, jobPtr);
             jobPtr.JobReport(JobStatus.DONE);
         }
     }
     catch (ThreadAbortException)
     {
         while (m_workPool.IsEmpty())
         {
             BaseJob jobPtr = m_workPool.Dequeue();
             jobPtr.JobReport(JobStatus.INCOMPLETE);
         }
     }
 }
Example #5
0
        /// <summary>
        /// Get response from the given uri with given credentials
        /// </summary>
        /// <param name="uri">uri</param>
        /// <param name="credentials">credentials</param>
        /// <param name="waitTimeInMilliSec">wait time in milliseconds</param>
        /// <returns></returns>
        public static String GetResponse(String uri, ICredentials credentials = null, int waitTimeInMilliSec = Timeout.Infinite)
        {
            if (uri == null || uri.Length == 0)
                throw new Exception("Must supply valid URI!");

            WebClient client = new WebClient(); WebRequest.Create(uri);
            string result = null;
            if (credentials != null)
                client.Credentials = credentials;
            EventEx doneEvent = new EventEx(false, EventResetMode.AutoReset);
            client.DownloadStringCompleted += (s, e) =>
            {
                result = e.Result;
                doneEvent.SetEvent();
            };
            client.DownloadStringAsync(new Uri(uri, UriKind.Absolute));
            doneEvent.WaitForEvent(waitTimeInMilliSec);
            return result;
        }
Example #6
0
        /// <summary>
        /// Download file from given uri to given filepath
        /// </summary>
        /// <param name="uri">uri</param>
        /// <param name="filepath">filepath</param>
        /// <param name="waitTimeInMilliSec">wait time in milliseconds</param>
        public static void DownloadFile(String uri, String filepath, int waitTimeInMilliSec=Timeout.Infinite)
        {
            if (uri == null || uri.Length == 0)
                throw new Exception("Must supply valid URI!");
            if (filepath == null || filepath.Length == 0)
                throw new Exception("Must supply valid filepath!");
            WebClient webClient = new WebClient();
            EventEx doneEvent = new EventEx(false, EventResetMode.AutoReset);
            webClient.OpenReadCompleted += (s, e) =>
            {
                try
                {
                    using (FileStream stream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
                    {
                        e.Result.CopyTo(stream);
                        stream.Flush();
                        doneEvent.SetEvent();
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                }
            };
            webClient.OpenReadAsync(new Uri(uri, UriKind.Absolute));
            doneEvent.WaitForEvent(waitTimeInMilliSec);
        }