C#Windows窗体启动/停止,循环不受限制
我在一个小项目,需要截图游戏。 我在游戏中做了截图,我需要让它开始和停止截图。 但我现在有个问题 代码: } 但是这样(用button1启动无限循环)我不能关闭它,因为循环正在运行 有什么想法吗?:)C#Windows窗体启动/停止,循环不受限制,c#,C#,我在一个小项目,需要截图游戏。 我在游戏中做了截图,我需要让它开始和停止截图。 但我现在有个问题 代码: } 但是这样(用button1启动无限循环)我不能关闭它,因为循环正在运行 有什么想法吗?:) 谢谢 相关:-你知道什么时候该离开循环吗。。。我的意思是当(true)保持它运行时,当你到达countScreens==20时,为什么不中断循环呢?改用计时器。@HansPassant你能给我举个例子吗? public string path = ""; public Form1()
谢谢 相关:-你知道什么时候该离开循环吗。。。我的意思是
当(true)
保持它运行时,当你到达countScreens==20
时,为什么不中断循环呢?改用计时器。@HansPassant你能给我举个例子吗?
public string path = "";
public Form1()
{
InitializeComponent();
}
private static string _path = "C:\\temp\\not posted";
private static int _timespan = 3000;
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = true;
var path =@"FPSS";
if (path != string.Empty)
_path = path;
_timespan = 3000;
DirectoryInfo dir = new DirectoryInfo(_path);
PrintScreen ps = new PrintScreen();
if (!dir.Exists) dir.Create();
var countScreens = 0;
while (true)
{
var task=StartPrinting(ps);
task.Wait();
Thread.Sleep(_timespan);
if (countScreens == 20)
{
System.GC.Collect();
countScreens = 0;
}
countScreens++;
}
}
private static async Task StartPrinting(PrintScreen ps)
{
var name = DateTime.Now.ToString("yyyyMMddhhmmss");
ps.CaptureScreenToFile($"{_path}\\{name}.png", ImageFormat.Png);
Console.WriteLine($"Printed {name}");
}
private void button2_Click(object sender, EventArgs e)
{
button1.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}