私はもう一度元気になりました。ランダムな時間間隔で無作為に実行を中断する方法
私のプログラムの2番目の部分として、ランダムな間隔でプログラムを一時停止できるようにする必要があります。たとえば、プログラムに10分、20分停止すると、47分が3分間停止します。ミリ秒...そのようなもの。私はこれでショットを試みるつもりはありませんが、私が見つけたすべてのものは、私の小さな脳が理解するにはあまりに複雑すぎるようです。誰かが私のためにそれを打ち破ることができれば、それは素晴らしいでしょう。
以下は私のコードです。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();
}
}
}
特定のメッセージボックスを削除していたときにコードが少し乱れている可能性がありますが、セミコロンがなくてもかまいません:) P – Vasu
*本当にアプリケーション全体のUIをブロックしますか?一時停止?ユーザーは*何でも*することができません。クローズをクリックすると動かず、動かなくなります。アクティブなコントロールなどを無効にするのが一般的ですが、UIスレッド自体は動作させてください。 –