Beispiel #1
0
        public static string Await(this ILineReader reader, [Implicit] ICallContext context, string text, TimeSpan timeout, bool removeFound = true)
        {
            System.Diagnostics.Debug.WriteLine("Reader.Await: " + text);
            if (context != null && context.LoggingEnabled)
            {
                context.Logger.Log("Await", "\"" + text + "\"");
            }
            var comparer = StringUtils.CreateComparer(text);

            reader.DebugDump();

            // If the reader has timestampe, set the timeout relative to the time of the current entry; otherwise just use current wall time.
            DateTime entry = (reader.LinesHaveTimestamp && reader.Current != null) ? reader.Current.Timestamp : DateTime.Now;

            // The time where the timeout expires.
            DateTime to = (timeout == TimeSpan.MaxValue) ? DateTime.MaxValue : entry + timeout;

            //bool sleep = false;
            do
            {
                var result = reader.Find(null, comparer, true);
                if (result != null)
                {
                    if (removeFound)
                    {
                        reader.Next();
                    }
                    return(result);
                }
                lock (reader.Sync)
                {
                    if (reader.Current == null)
                    {
                        Monitor.Wait(reader.Sync, 50);
                    }
                }
                //sleep = true;
            } while (DateTime.Now.TimeTill(to) > TimeSpan.Zero);

            if ((context as IScriptCallContext) != null)
            {
                var ctx        = context as IScriptCallContext;
                var readerName = reader.Source.Name;
                if (readerName == null)
                {
                    readerName = "log reader";
                }
                ctx.ReportFailure($"No entry matching \"{text}\" was found in {readerName}.");
            }
            return(null);
        }