在 c# Windows 服务中获取鼠标位置不起作用

问题描述 投票:0回答:0

我首先构建了一个 c# 控制台应用程序,目前每 30 秒检查一次自上次检查/(程序启动)以来鼠标位置是否发生了变化。如果是,什么都不应该发生,只是计时器应该重新启动。如果现在鼠标移动被识别,应该调用一些函数。

这一切在控制台应用程序中都运行良好。 现在我将其复制到 c# Windows 服务应用程序,但鼠标移动无关紧要,它会在每次函数调用时调用,当计时器已过时。

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Drawing;
using System.Timers;
using System.Runtime.InteropServices;
using System.IO;

using System.Windows.Forms;
using System.Runtime;
using System.Security.AccessControl;
using System.Security.Cryptography.X509Certificates;
using System.Windows;
using System.Windows.Input;

namespace WindowsService1
{
    static class Program
    {
        private static System.Timers.Timer aTimer;
        static Point Position = new Point();
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new Service1()
            };
            SetTimer();
            ServiceBase.Run(ServicesToRun);
        }

        static void getMousePosition()
        {
            Position.X = Control.MousePosition.X;
            Position.Y = Control.MousePosition.Y;
        }


        static void DeleteDownload()
        {
            // aus welchem Ordner die Daten gelöscht werden sollen
            string directory = @"C:\folder2";
            //string directory = @"C:\Users\" + Environment.UserName + "/Downloads";

            try
            {
                // alle inhalte aus dem Ordner auflisten
                string[] fileList = Directory.GetFiles(directory);

                // Für jeden File in der Liste
                foreach (string file in fileList)
                {
                    // Lösche den File
                    File.Delete(file);
                    Console.WriteLine(file + " Deleted");
                }
            }
            catch (DirectoryNotFoundException directoryNotFound)
            {
                Console.WriteLine(directoryNotFound.Message);
            }

            string[] DirList = Directory.GetDirectories(directory);

            try
            {
                foreach (string dir in DirList)
                {
                    Directory.Delete(dir, true);
                    Console.WriteLine(dir + " Deleted");
                }
            }
            catch (DirectoryNotFoundException directoryNotFound)
            {
                Console.WriteLine(directoryNotFound.Message);
            }
            Console.WriteLine("Downloads geleert");
        }


        [DllImport("Shell32.dll")]
        static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlag dwFlags);
        enum RecycleFlag : int
        {
            SHERB_NOCONFIRMATION = 0x00000001, // No confirmation, when emptying
            SHERB_NOPROGRESSUI = 0x00000001, // No progress tracking window during the emptying of the recycle bin
            SHERB_NOSOUND = 0x00000004 // No sound when the emptying of the recycle bin is complete
        }
        static void CleanrecyleBin()
        {
            SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlag.SHERB_NOSOUND | RecycleFlag.SHERB_NOCONFIRMATION);
            Console.WriteLine("Recyle Bin geleert");
        }


        static void SetTimer()
        {
            getMousePosition();
            aTimer = new System.Timers.Timer(30000);
            aTimer.Elapsed += OnTimedEvent;
            aTimer.AutoReset = false;
            aTimer.Enabled = true;
            Console.ReadLine();
        }
        static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            // Aktuelle Maus Position ermitteln
            Point CurrentPosition = new Point();
            CurrentPosition.X = Control.MousePosition.X;
            CurrentPosition.Y = Control.MousePosition.Y;

            bool interaction = false;
            //MessageBox.Show(CurrentPosition.X + " ; " + Position.X);
            //MessageBox.Show(CurrentPosition.Y + " ; " + Position.Y);
            //if (CurrentPosition.X == Position.X && CurrentPosition.Y == Position.Y)
            if (CurrentPosition.X != Position.X || CurrentPosition.Y != Position.Y)
                interaction = true;

            if (interaction == false)
            //if (1!=1)
            {
                Console.WriteLine("Daten werden gelöscht");
                DeleteDownload();
                //CleanrecyleBin();
                //CheckUsername();
                SetTimer();
            }
            else
            {
                Console.WriteLine("Timer wird neugestartet");
                SetTimer();
            }
        }


        static void CheckUsername()
        {
            if (Environment.UserName != "z004rbke")
            {
                Console.WriteLine($"Username: {Environment.UserName}");
                MessageBox.Show("PC wird neugestartet");
                Process Neustart = Process.Start(@"shutdown", "-r -t 1");
            }
            else
            {
                Console.WriteLine("PC wird nicht neugestartet");
            }
        }
    }
}
c# windows-services mouse
© www.soinside.com 2019 - 2024. All rights reserved.