如何以随机间隔暂停执行随机时间

时间:2011-08-14 06:01:24

标签: c# multithreading

我又回来了......已经。

作为我程序的第二部分,我需要能够以随机间隔暂停我的程序 - 例如,程序10分钟后暂停20秒,其中47分钟暂停3分钟49毫秒......那样的事情。我不打算尝试射击,因为我发现的所有东西似乎都太复杂了,我的小脑子也无法理解,所以如果有人能为我分解它,那就太棒了。 / p>

下面是我的代码,我需要在for循环中暂停一下。谢谢! 另外,我知道我已经点击,睡觉,点击,睡觉,然后重复循环 - 我想要它:)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;

namespace MagicMouse
{
    public partial class Form1 : Form
    {
        //all this stuff has to do with being able to actually click a mouse
        //[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
        //public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

        [DllImport("user32.dll")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;

        //this function will click the mouse using the parameters assigned to it
        public void DoMouseClickLeft()
        {
            //Call the imported function with the cursor's current position
            int X = Cursor.Position.X;
            int Y = Cursor.Position.Y;
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
        }

        public void NumberCheck(string ValidateNum)
        {
            long CanConvertOut = 0;
            bool CanConvertBool = long.TryParse(ValidateNum, out CanConvertOut);
            if (CanConvertBool == false)
            {
                MessageBox.Show("Please enter a valid number.");
                return;
            }
        }

        //this generates the random number used for sleep timer
        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        //button 1 is start clicking
        private void button1_Click(object sender, EventArgs e)
        {
            //this section sends data to the number validation function
            string ValidateNum = NumberOfItems.Text;
            NumberCheck(ValidateNum);
            int Clicks = Convert.ToInt16(NumberOfItems.Text);

            for (int i = 1; i < Clicks; i++)
            {
                int SleepTime1 = RandomNumber(819, 1092);
                DoMouseClickLeft();
                Thread.Sleep(SleepTime1);
                DoMouseClickLeft();
                Thread.Sleep(SleepTime1);
            }
        }

        //button 2 is exit
        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();

        }


    }
}

2 个答案:

答案 0 :(得分:2)

尝试这样的事情:

private DateTime GetNextSleep()
{
    // Between 3 and 20 minutes, adjust as needed.
    return DateTime.Now + new TimeSpan(0, 0,
        RandomNumber(60 * 3, 60 * 20));
}

private void button1_Click(object sender, EventArgs e)
{
    //this section sends data to the number validation function
    string ValidateNum = NumberOfItems.Text;
    NumberCheck(ValidateNum);
    int Clicks = Convert.ToInt16(NumberOfItems.Text);

    DateTime nextSleep = GetNextSleep();

    for (int i = 1; i < Clicks; i++)
    {
        int SleepTime1 = RandomNumber(819, 1092);
        DoMouseClickLeft();
        Thread.Sleep(SleepTime1);
        DoMouseClickLeft();
        Thread.Sleep(SleepTime1);

        if (DateTime.Now >= nextSleep)
        {
            // Sleep between 1 and 5 minutes, adjust as needed.
            Thread.Sleep(new TimeSpan(0, 0,
                RandomNumber(60 * 1, 60 * 5)));

            nextSleep = GetNextSleep();
        }
    }
}

基本上,您预先计算要暂停的时间。一旦到达那个时间,你会睡一段随机的时间,计算下一个睡眠时间,然后返回循环。

答案 1 :(得分:0)

        using (var taskHandle = new ManualResetEvent(false))
        {
            taskHandle.WaitOne(wait * 1000);
        }

比Thread.Sleep更好的方法(等待* 1000);