2011-08-14 56 views
1

我又回到了......已经。如何暂停执行随机时间间隔

作为我的程序的第二部分,我需要能够随机暂停我的程序 - 例如,程序进入10分钟后暂停20秒,其中47分钟暂停3分钟49毫秒......类似的东西。我不打算尝试一下,因为我发现的所有东西似乎都太复杂了,以至于我的小脑袋不能理解,所以如果有人能为我分解它,那将会很棒。

下面是我的代码,我需要暂停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(); 

     } 


    } 
} 
+0

当我删除某些消息框时,我可能会弄乱一些代码,但是它只应该是一个分号丢失:):P – Vasu

+0

您是否真的想要在应用程序暂停?用户将无法做任何事情*点击关闭不会工作,移动它不会工作等。通常禁用任何活动控件(等),但保持UI线程本身工作。 –

回答

1

尝试是这样的:

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(); 
     } 
    } 
} 

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

+0

嗨cdhowie,这似乎是一个很好的解决方案,但是,一个noob想法浮现在脑海。上次我玩时间(哈哈),把时间放在一个循环中的函数大大减慢了我的程序。这是以不同的方式工作,还是会经历相同类型的滞后? – Vasu

+0

是的,'DateTime.Now'访问器是一个相对昂贵的调用。如果这很重要,你可以考虑调整你的'SleepTime1'变量的随机范围来补偿。从它的外观来看,你不会选择特定的时机,所以我不确定这会影响你的代码。解决方法是检查每次* N *次迭代的时间(检查'i%N == 0'),这将减少'DateTime.Now'调用的次数。选择适当提高性能的* N *,而不会在睡眠时间上失去太多的精度。 – cdhowie

+0

是否可以在“N”次迭代后随机暂停程序?我不认为这将需要datetime.now访问器,因为它将通过使用tread.sleep()来实现。从本质上讲,每次迭代都是按照自己的时间进行的,因为从长远来看,每次迭代应持续约2秒。 – Vasu

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

是比Thread.Sleep(wait * 1000)更好的方法;