Beispiel #1
0
        public void GivenAddedProcessWatchWithNoCapture_WhenStartCapture_CaptureDeviceFilterDoesntChange()
        {
            Process         runningProcess = ProcessWatchTestHelper.GivenFirstRunningProcess();
            IProcessWatcher watchdog       = GivenProcessWatcher();

            watchdog.AddProcessToWatchList(runningProcess.ProcessName, false);
            watchdog.StartCapture();

            WatchJobHandler.Invoke(null, new EventArgs() as ElapsedEventArgs);

            Assert.Equal(string.Empty, CaptureDeviceFilter);
        }
Beispiel #2
0
 /// <summary>
 /// 循环调用某个无返回值的方法
 /// </summary>
 /// <param name="time">毫秒</param>
 /// <param name="timeout">超时时间</param>
 /// <param name="con">基于某个控件的方法</param>
 /// <param name="e">执行事件</param>
 public static void TimersMethod(int time, int timeout, Control con, ElapsedEventHandler e)
 {
     try {
         // 延迟1毫秒执行方法
         System.Timers.Timer timer = new System.Timers.Timer(time);
         int tt = 0;
         ElapsedEventHandler handler = (object sender, ElapsedEventArgs e1) => {
             if (timeout != -1 && tt >= timeout)
             {
                 timer.Dispose();
             }
             if (con != null)
             {
                 if (con.InvokeRequired)
                 {
                     con.Invoke(new EventHandler(delegate(object obj, EventArgs e2){
                         e.Invoke(sender, e1);
                     }));
                 }
             }
             else
             {
                 e.Invoke(sender, e1);
             }
             tt += time;
         };
         // 到达时间的时候执行事件;
         timer.Elapsed += handler;
         // 设置是执行一次(false)还是一直执行(true);
         timer.AutoReset = true;
         // 是否执行System.Timers.Timer.Elapsed事件;
         timer.Enabled = true;
     } catch (Exception ex) {
         Console.WriteLine(ex);
     }
 }
Beispiel #3
0
    //
    // When base timer fires Elapsed event, my ElapsedEventHandler will be called.
    // Inside, set IsElapsedEventRunning = true, then call the user's event handler.
    // When finished, set IsElapsedEventRunning = false
    //
    void MyTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        //
        // Warning!  Not thread safe!  Not reentrant!
        //
        System.Diagnostics.Debug.WriteLine("MyTimer.IsElapsedEventRunning = {0}", IsElapsedEventRunning);

        IsElapsedEventRunning = true;

        // Is Invoke ok here or is BeginInvoke/EndInvoke required?
        elapsed.Invoke(sender, e);

        IsElapsedEventRunning = false;

        System.Diagnostics.Debug.WriteLine("MyTimer.IsElapsedEventRunning = {0}", IsElapsedEventRunning);
    }
Beispiel #4
0
        public void Create <T>(StreamReader sreader, Orion.IO.BinaryWriter <T> bwriter, char delimiter, Func <string[], T> TokenParser) where T : struct, IDateStructure
        {
            T   att        = new T();
            int rowcount   = 0;
            int currentrow = 0;

            // Remove first line
            sreader.ReadLine();
            while (!sreader.EndOfStream)
            {
                sreader.ReadLine();
                rowcount++;
            }
            Console.WriteLine("Row Count = {0}", rowcount);

            // Reset stream
            sreader.BaseStream.Position = 0;
            // Remove first line
            sreader.ReadLine();

            var action = new ElapsedEventHandler((s, e) => {
                Console.WriteLine("Proccesed {0}/{1} Rows - {2:P2}", currentrow, rowcount, (float)currentrow / (float)rowcount);
            });

            Timer timer = new Timer();

            timer.Elapsed += action;
            timer.Interval = 1000;
            timer.Start();
            while (!sreader.EndOfStream)
            {
                // Read entire row as string and tokenize
                string[] tokens = sreader.ReadLine().Split(delimiter);

                //Parse tokens
                att = TokenParser.Invoke(tokens);

                bwriter.Write(att);

                currentrow++;
            }
            action.Invoke(null, null);
            timer.Stop();
            timer.Close();
        }
Beispiel #5
0
        private void addTouchEventToButton(ButtonStroked button, Timer repeatTimer, ElapsedEventHandler action, bool changeGameTimer)
        {
            repeatTimer.Elapsed += action;
            repeatTimer.AutoReset = true;
            button.Touch +=
                delegate(object sender, View.TouchEventArgs e)
            {
                e.Handled = false; // so the button can change it's state
                switch (e.Event.Action)
                {
                case MotionEventActions.Up:
                    // The user release the button so we stop the timer
                    repeatTimer.Stop();
                    if(changeGameTimer)
                    {
                        _gameTimer.Start();
                        _player1View.Draw(); //update score
                    }
                    break;
                case  MotionEventActions.Down:
                    if( !repeatTimer.Enabled)
                    {
                        repeatTimer.Start();
                        if(changeGameTimer)
                        {
                            _gameTimer.Stop();
                        }
                    }
                    action.Invoke(null, null);
                    break;
                case MotionEventActions.Move:
                    int x = (int) e.Event.GetX();
                    int y = (int) e.Event.GetY();

                    // Be lenient about moving outside of buttons
                    int slop = ViewConfiguration.Get(button.Context).ScaledTouchSlop;
                    if ((x < 0 - slop) || (x >= button.Width + slop) || (y < 0 - slop) || (y >= button.Height + slop))
                    {
                        repeatTimer.Stop();
                        if(changeGameTimer)
                        {
                            _gameTimer.Start();
                            _player1View.Draw(); //update score
                        }
                    }
                    break;
                }
            };
        }
Beispiel #6
0
 /// <summary>
 ///     Callback, called when the timer.
 /// </summary>
 /// <param name="uTimerID"> Identifier for the timer. </param>
 /// <param name="uMsg">     The message. </param>
 /// <param name="dwUser">   The user. </param>
 /// <param name="dw1">      The first dw. </param>
 /// <param name="dw2">      The second dw. </param>
 private void TimerCallback(uint uTimerID, uint uMsg, UIntPtr dwUser, UIntPtr dw1, UIntPtr dw2)
 {
     _elapsed?.Invoke(this);
 }
Beispiel #7
0
 private void OnElapsed(object sender, ElapsedEventArgs e)
 {
     _onIntervalElapsed.Invoke(sender, e);
 }
Beispiel #8
0
        private static void CsvToBinaryFile <T>(string[] sources, string destination, char delimiter, bool isFirstLineHeader)
            where T : TripRecordModel, new()
        {
            BinaryWriter <TripRecord> writer = new BinaryWriter <TripRecord>(destination);
            T model = new T();

            foreach (var source in sources)
            {
                Console.WriteLine(">Processing {0}<", source);
                using (StreamReader sreader = new StreamReader(source))
                {
                    TripRecord record     = new TripRecord();
                    int        rowcount   = 0;
                    int        currentrow = 0;

                    // Remove first line
                    if (isFirstLineHeader)
                    {
                        sreader.ReadLine();
                    }
                    while (!sreader.EndOfStream)
                    {
                        sreader.ReadLine();
                        rowcount++;
                    }
                    Console.WriteLine("Row Count = {0}", rowcount);

                    Timer timer  = new Timer(1000);
                    var   action = new ElapsedEventHandler((s, e) =>
                    {
                        Console.CursorLeft = 0;
                        Console.Write("Proccesed {0}/{1} Rows - {2:P2}\t", currentrow, rowcount, (float)currentrow / (float)rowcount);
                    });
                    timer.Elapsed += action;
                    timer.Start();

                    Dictionary <string, string> row = new Dictionary <string, string>();
                    sreader.BaseStream.Position = 0;  // Reset stream
                    string[] header = sreader.ReadLine().Split(delimiter);

                    while (!sreader.EndOfStream)
                    {
                        // Read entire row as string and tokenize
                        string[] tokens = sreader.ReadLine().Split(delimiter);
                        Debug.Assert(row.Count != tokens.Length);

                        for (int i = 0; i < tokens.Length; i++)
                        {
                            row.Add(header[i], tokens[i]);
                        }

                        //Parse tokens
                        record = model.ParseTokens(row);

                        writer.Write(record);

                        currentrow++;
                    }
                    action.Invoke(null, null);
                    timer.Stop();
                    timer.Close();
                }
                writer.Flush();
            }
            writer.Close();
        }
Beispiel #9
0
        private static TripRecordFactory CsvToBinaryFileWithLookupTable <T>(string[] sources, string destination, char delimiter)
            where T : TripRecordModel, new()
        {
            BinaryWriter <TripRecord> writer  = new BinaryWriter <TripRecord>(destination);
            TripRecordFactory         context = new TripRecordFactory(destination);
            T model = new T();

            foreach (var source in sources)
            {
                Console.WriteLine(">Processing {0}<", source);
                using (StreamReader sreader = new StreamReader(source))
                {
                    TripRecord record     = new TripRecord();
                    int        rowcount   = 0;
                    int        currentrow = 0;

                    //Count Rows
                    while (!sreader.EndOfStream)
                    {
                        sreader.ReadLine();
                        rowcount++;
                    }
                    Console.WriteLine("Row Count = {0}", rowcount);

                    Timer timer  = new Timer(1000);
                    var   action = new ElapsedEventHandler((s, e) =>
                    {
                        Console.CursorLeft = 0;
                        Console.Write("Proccesed {0}/{1} Rows - {2:P2}\t", currentrow, rowcount, (float)currentrow / (float)rowcount);
                    });
                    timer.Elapsed += action;
                    timer.Start();

                    Dictionary <string, string> row = new Dictionary <string, string>();
                    sreader.BaseStream.Position = 0;  // Reset stream
                    string[] header = sreader.ReadLine().ToLower().Split(delimiter);

                    while (!sreader.EndOfStream)
                    {
                        row.Clear();
                        // Read entire row as string and tokenize
                        string[] tokens = sreader.ReadLine().Split(delimiter).Take(header.Length).ToArray();

                        for (int i = 0; i < tokens.Length; i++)
                        {
                            row.Add(header[i], tokens[i]);
                        }

                        //Parse tokens
                        record = model.ParseTokens(row);

                        //Write data structure to file
                        writer.Write(record);

                        //Index the data structure
                        DateTime    key = new DateTime(record.TimeStamp.Year, record.TimeStamp.Month, record.TimeStamp.Day);
                        List <long> ret;
                        if (!context.LookupTable.TryGetValue(key, out ret))
                        {
                            ret = new List <long>();
                            context.LookupTable[key] = ret;
                        }
                        ret.Add(record.ID);

                        currentrow++;
                    }
                    action.Invoke(null, null);
                    Console.WriteLine();
                    timer.Stop();
                    timer.Close();
                }
                writer.Flush();
            }
            writer.Close();

            //Save Lookup table to file
            string path     = Path.GetDirectoryName(destination);
            string name     = Path.GetFileNameWithoutExtension(destination);
            string filename = Path.Combine(path, string.Format("{0}.{1}", name, Constants.LookupTableExtension));

            Console.WriteLine(">Storing Lookup Table<");

            BinaryWriter <LookupRow> stream = new BinaryWriter <LookupRow>(filename);

            foreach (var entry in context.LookupTable)
            {
                foreach (var value in entry.Value)
                {
                    LookupRow row = new LookupRow(entry.Key, value);
                    stream.Write(row);
                }
                stream.Flush();
            }
            stream.Close();
            Console.WriteLine("Lookup Table Successfully stored at {0}", filename);

            return(context);
        }
Beispiel #10
0
        /// <summary>
        /// Creates a timer with given interval that will work to pull data from exchange markets
        /// </summary>
        /// <param name="name">Name of the puller</param>
        /// <param name="seconds">interval in seconds</param>
        /// <param name="delaySeconds">Wait before starting puller</param>
        /// <param name="onPullingHandler">Handler to be run while pulling</param>
        protected void CreatePuller(string name, int seconds, int delaySeconds, Func <PullerSession, Task> onPullingHandler, bool needsPuller = true)
        {
#if DEBUG
            if (!Tester.IsServiceTesting(this, name))
            {
                return;
            }
#endif
            Task.Factory.StartNew(() =>
            {
                if (delaySeconds > 0)
                {
                    Thread.Sleep(delaySeconds * 1000);
                }

                var timer  = new Puller(seconds * 1000);
                timer.Name = name;
                ElapsedEventHandler onTimerElapsed = (s, a) =>
                {
                    var puller = (Puller)s;
                    try
                    {
                        if (puller.IsPulling)
                        {
                            Console.WriteLine(Name + " " + puller.Name + ": Still running, skipping...");
                        }
                        else
                        {
                            puller.IsPulling = true;
                            if (SocketManager.Maintenence)
                            {
                                Console.WriteLine(Name + " " + puller.Name + ": Waiting maintenance...");
                            }
                            SocketManager.MaintenenceEvent.WaitOne();
                            //var t = new System.Timers.Timer(60000 * 2);
                            //t.Elapsed += (sender, args) =>
                            //{
                            //    puller.IsPulling = false;
                            //    try
                            //    {
                            //        t.Stop();
                            //        t.Dispose();
                            //    }
                            //    catch (Exception e)
                            //    {
                            //    }
                            //};
                            //t.Start();
                            Console.WriteLine(Name + " " + puller.Name + ": Running...");
                            if (needsPuller)
                            {
                                using (var session = PullerSession.Create())
                                {
                                    onPullingHandler(session).Wait();
                                }
                            }
                            else
                            {
                                onPullingHandler(null).Wait();
                            }
                            //try
                            //{
                            //    t.Stop();
                            //    t.Dispose();
                            //}
                            //catch (Exception e)
                            //{
                            //}
                            puller.IsPulling = false;
                            Console.WriteLine(Name + " " + puller.Name + ": Done");
                        }
                    }
                    catch (Exception e)
                    {
                        puller.IsPulling = false;
                        Console.WriteLine(e);
                    }
                };
                timer.Elapsed += onTimerElapsed;
                lock (Pullers)
                {
                    Pullers[name] = timer;
                }
                timer.Start();
                onTimerElapsed.Invoke(timer, null);
            });
        }