问题:如何正确处理方法中创建的 ScreenSaverForm(s) ShowScreensaver
?
当我从 Visual Studio 运行代码分析时,它会报告以下内容:In method 'Program.ShowScreenSaver()', call System.IDisposable.Dispose on object 'ssf' before all references to it are out of scope
.
我不明白该怎么做,因为应用程序在退出方法后进入匿名消息循环ShowScreenSaver()
。只有屏幕保护程序本身可以通过调用Application.Exit()
mousemove-event 来停止应用程序。屏幕保护程序表单是否应该自行处理?
static class Program
{
[STAThread]
static void Main(string[] args)
{
ShowScreenSaver();
Application.Run();
}
/// <summary>
/// Display the form on each of the computer's monitors.
/// </summary>
static void ShowScreenSaver()
{
ScreenSaverForm ssf;
foreach (Screen screen in Screen.AllScreens)
{
ssf = new ScreenSaverForm(screen.Bounds);
ssf.Show();
}
}
}
屏幕保护程序类的简化代码
public partial class ScreenSaverForm : Form
{
public ScreenSaverForm()
{
InitializeComponent();
//Display pretty image
ShowprettyImage();
}
private void ScreenSaverForm_MouseMove(object sender, MouseEventArgs e)
{
if (!mouseLocation.IsEmpty)
{
// Terminate if mouse is moved a significant distance
if (Math.Abs(mouseLocation.X - e.X) > 25 ||
Math.Abs(mouseLocation.Y - e.Y) > 25)
Application.Exit();
}
// Update current mouse location
mouseLocation = e.Location;
}
}