Exemple #1
0
 public static int Main()
 {
     //Single-instance only
     using (var mutex = new Mutex(false, string.Format("Global\\{{{0}}}",
                                                       ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false)
                                                        .GetValue(0)).Value.ToString())))
     {
         var hasHandle = false;
         try
         {
             var previousExitedNormally = true;
             try
             {
                 hasHandle = mutex.WaitOne(5000, false);
                 if (!hasHandle)
                 {
                     return((int)ExitCodes.AlreadyRunning);
                 }
             }
             catch (AbandonedMutexException)
             {
                 hasHandle = true;
                 previousExitedNormally = false;
             }
             try
             {
                 InstanceMain(previousExitedNormally);
                 return((int)ExitCodes.OK);
             }
             catch (System.Runtime.Serialization.SerializationException)
             {
                 return((int)ExitCodes.SerializationError);
             }
             catch (FileNotFoundException)
             {
                 return((int)ExitCodes.FileNotFound);
             }
             catch (IOException)
             {
                 return((int)ExitCodes.FileIOError);
             }
             catch (Exception ex)
             {
                 ErrorListener.AddFormat(ex, "Unhandled exception!");
             }
             return((int)ExitCodes.UnhandledException);
         }
         finally
         {
             if (hasHandle)
             {
                 mutex.ReleaseMutex();
             }
         }
     }
 }
Exemple #2
0
        private int ExecutionEngine(CancellationTokenSource cancel = null)
        {
            Rectangle window = new Rectangle();

            if (Properties.Settings.Default.DisableWindowFilter)
            {
                window = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
            }
            else
            {
                //First, look for the window needed
                IntPtr hWnd = IntPtr.Zero;
                try
                {
                    if (Program.WindowTitleString.StartsWith(Serialization.HandlePrefix))
                    {
                        hWnd = (IntPtr)int.Parse(Program.WindowTitleString.Remove(0, Serialization.HandlePrefix.Length),
                                                 System.Globalization.NumberStyles.HexNumber);
                    }
                    else
                    {
                        hWnd = Native.FindWindowByCaption(Program.WindowTitleString);
                    }
                }
                catch (Exception ex)
                {
                    ErrorListener.Add(ex);
                }
                if (hWnd == IntPtr.Zero)
                {
                    return((int)ScenarioExitCodes.WindowNotFound);
                }
                //TODO: Next, try to bring it to front and make active

                //Next, see if it's a 0x0 rectangle (still minimized or smth)
                try
                {
                    window = Native.GetWindowRectangle(hWnd);
                }
                catch (Exception ex)
                {
                    ErrorListener.Add(ex);
                }
                if (window.IsEmpty)
                {
                    return((int)ScenarioExitCodes.EmptyWindow);
                }
            }
            //Finally, execute the scenario
            try
            {
                var dic = Program.Database.ToDictionary(x => x.Name);
                for (int i = 0; i < Actions.Length; i++)
                {
                    if (cancel != null)
                    {
                        if (cancel.IsCancellationRequested)
                        {
                            return((int)ScenarioExitCodes.Aborted);
                        }
                    }
                    if (CheckKeyPressed(LoopBreakKey))
                    {
                        return((int)ScenarioExitCodes.Aborted);
                    }
                    switch (Actions[i].Type)
                    {
                    case ActionTypes.MouseClick:
                        MouseClick(dic[(string)Actions[i].Arguments[0]], window);
                        break;

                    case ActionTypes.RightMouseClick:
                        RightMouseClick(dic[(string)Actions[i].Arguments[0]], window);
                        break;

                    case ActionTypes.LeftMouseDoubleClick:
                        LeftMouseDoubleClick(dic[(string)Actions[i].Arguments[0]], window);
                        break;

                    case ActionTypes.PressKey:
                        PressKey((WindowsInput.Native.VirtualKeyCode)Actions[i].Arguments[0]);
                        break;

                    case ActionTypes.WaitForPixel:
                    {
                        bool r = WaitForPixel(dic[(string)Actions[i].Arguments[0]], window, (Color)Actions[i].Arguments[1],
                                              cancel, (Actions[i].Arguments.Length > 2) ? (int)Actions[i].Arguments[2] : 0);
                        if (FailOnTimeout && !r)
                        {
                            throw new TimeoutException();
                        }
                        break;
                    }

                    case ActionTypes.Sleep:
                        Sleep((int)Actions[i].Arguments[0]);
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (InvalidCastException ex)
            {
                ErrorListener.AddFormat(ex, "Wrong arguments specified for an action!");
                return((int)ScenarioExitCodes.WrongArguments);
            }
            catch (TimeoutException)
            {
                ErrorListener.Add(new Exception("Timed out during waiting for pixel color (FailOnTimeout is set to True)."));
                return((int)ScenarioExitCodes.Timeout);
            }
            catch (Exception ex)
            {
                ErrorListener.Add(ex);
                return((int)ScenarioExitCodes.UnexpectedError);
            }
            return((int)ScenarioExitCodes.OK);
        }