我试图在 1 秒内尽可能多地调用一个方法,所以我决定使用计时器来帮助执行此操作,但是当计时器运行滴答事件处理程序时(1 秒后),该方法仍然被调用 -我已经开始如下:
public partial class Form1 : Form
{
public static Timer prntScreenTimer = new Timer();
public Form1()
{
InitializeComponent();
startCapture();
}
private static void startCapture()
{
prntScreenTimer.Tick += new EventHandler(prntScreenTimer_Tick);
prntScreenTimer.Start();
prntScreenTimer.Interval = 1000;
while (prntScreenTimer.Enabled)
{
captureScreen();
}
}
private static void prntScreenTimer_Tick(object sender, EventArgs e)
{
prntScreenTimer.Stop();
}
private static void captureScreen()
{
int ScreenWidth = Screen.PrimaryScreen.Bounds.Width;
int ScreenHeight = Screen.PrimaryScreen.Bounds.Height;
Graphics g;
Bitmap b = new Bitmap(ScreenWidth, ScreenHeight);
g = Graphics.FromImage(b);
g.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.Bounds.Size);
// Draw bitmap to screen
// pictureBox1.Image = b;
// Output bitmap to file
Random random = new Random();
int randomNumber = random.Next(0, 10000);
b.Save("printScrn-" + randomNumber, System.Drawing.Imaging.ImageFormat.Bmp);
}
}
}