几天后程序冻结

Program freeze after few days

我创建了一个小程序来创建一个日志文件来记录人们的身份证号码,到目前为止它运行良好,没有任何问题或错误,但最近我注意到 运行 三天后它冻结了另一个程序,直到我强制关闭它。任何人都可以查看代码以查看它是否有任何问题或改进代码。谢谢。

这些程序适用于 .NET Frameworks 3.5,适用于 Windows XP 系统,如果可能的话,可以使其适用于较低版本的 .NET Framework 以减少额外文件的安装。

MainWin 窗体创建全屏 window 到 mask/cover 其他软件的一些元素。设置为最顶层以始终位于所有内容的顶部。它在一个文本文件中有一个透明部分,然后它最小化 window 并最终激活一个计时器。当计时器结束时,它再次最大化 window。它有一个打开 LoginWin 窗体的按钮和一个清除 serieBox 中的数据的按钮,return 将光标移动到文本框。

LoginWin窗体是window输入登录信息打开LogFileWin窗体。

LogFileWin 窗体是一个window 从richTextBox 中的文本文件中读取保存的数据,此数据来自MainWin 窗体。它有一个关闭按钮和一个按钮,用于打开 FolderBrowserDialog 以将文本文件保存在另一个位置或可移动存储设备。

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.IO;

namespace LogSerie
{
    public partial class MainWin : Form
    {

        public MainWin()
        {
            InitializeComponent();

        }

        private void MainWin_Load(object sender, EventArgs e)
        {
            this.TopMost = true;
        }

        private void serieBox_KeyDown(object sender, KeyEventArgs e)
        {
            this.serieBox.MaxLength = 10;
            if (e.KeyCode == Keys.Enter)
            {
                if ((serieBox.Text != ""))
                {

                    if (serieBox.Text == "WLMANTO")
                    {
                        StreamWriter B = new StreamWriter("LogfileOperator.txt", true);
                        B.WriteLine(DateTime.Now + "  " + label1.Text + " " + serieBox.Text);
                        B.Close();
                        serieBox.Clear();
                        this.WindowState = FormWindowState.Minimized;
                        timerManto.Enabled = true;
                    }
                    else
                    {
                        StreamWriter A = new StreamWriter("LogfileOperator.txt", true);
                        A.WriteLine(DateTime.Now + "  " + label1.Text + " " + serieBox.Text);
                        A.Close();
                        serieBox.Clear();
                        this.WindowState = FormWindowState.Minimized;
                        timerOperador.Enabled = true;
                    }
                }
            }
        }


        private void timerOperador_Tick(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            timerOperador.Enabled = false;
        }

        private void timerManto_Tick(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            timerManto.Enabled = false;
        }

        private void logButton_Click(object sender, EventArgs e)
        {
            LoginWin openForm = new LoginWin();
            openForm.ShowDialog();
        }

        private void borrarBut_Click(object sender, EventArgs e)
        {
            serieBox.Clear();
            serieBox.Select();
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LogSerie
{
    public partial class LoginWin : Form
    {
        public LoginWin()
        {
            InitializeComponent();
        }

        private void LoginWin_Load(object sender, EventArgs e)
        {
            this.TopMost = true;
        }

        private void entrarBut_Click(object sender, EventArgs e)
        {
            if ((usuBox.Text != "") && (contraBox.Text != ""))
            {
                if ((usuBox.Text == "ADMIN") && (contraBox.Text == "PASS"))
                {
                    LogFileWin openForm = new LogFileWin();
                    openForm.TopMost = true;
                    openForm.ShowDialog();
                    usuBox.Clear();
                    contraBox.Clear();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Login Incorrect", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void cancelBut_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace LogSerie
{
    public partial class LogFileWin : Form
    {
        public LogFileWin()
        {
            InitializeComponent();
        }

        private void LogFileWin_Load(object sender, EventArgs e)
        {
            this.TopMost = true;
        }

        private void logfileButton_Click(object sender, EventArgs e)
        {
            string path = @"C:\LogfileOperator\LogfileOperator.txt";
            StreamReader stream = new StreamReader(path);
            string filedata = stream.ReadToEnd();
            richTextBox1.Text = filedata.ToString();
            stream.Close();
        }

        private void closeBut_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void usbBut_Click(object sender, EventArgs e)
        {
            string fileName = "LogfileOperator.txt";
            string sourcePath = @"C:\LogfileOperator";
            using (FolderBrowserDialog ofd = new FolderBrowserDialog())
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    FileInfo fileInfo = new FileInfo(fileName);
                    sourcePath = Path.Combine(ofd.SelectedPath, fileInfo.Name);
                    File.Copy(fileName, sourcePath, true);
                    MessageBox.Show("Logfile Saved", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
}

如果您被迫使用这个旧的 OS 和旧框架,您将会遇到像这样的奇怪错误。您也许可以通过减少资源使用来解决它。

您不断地创建表单 LoginWin 和 LogFileWin 的新副本,这需要 OS 资源。而是为每个表单创建 一个 实例并 re-use 它们。

您的代码中也没有任何异常处理,因此如果文件不存在、权限更改或诸如此类的事情,您可能会遇到问题。您需要进行异常处理,这将在问题发生时为您提供有关问题的更多信息。

要 re-use 表单,请在 class:

中创建一个实例作为私有字段
public partial class LoginWin : Form
{
    // store the LogFileWin form so that we can re-use it
    private LogFileWin _logFileWin;

    public LoginWin()
    {
        InitializeComponent();

        _logFileWin = new LogFileWin(); { TopMost = true; }
    }

    private void LoginWin_Load(object s, EventArgs e) { TopMost = true; }

    private void entrarBut_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(usuBox.Text) ||
            string.IsNullOrEmpty(contraBox.Text))
        {
            return;
        }

        if ((usuBox.Text == "ADMIN") &&
            (contraBox.Text == "PASS"))
        {
            // add a reset function to the form
            // that makes it ready to display again
            _logFileWin.Reset();

            // show the dialog
            _logFileWin.ShowDialog();

            usuBox.Clear();
            contraBox.Clear();
            this.Close();
        }
        else
        {
            MessageBox.Show("Login Incorrect",
                            "Message",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
        }
    }

    private void cancelBut_Click(object s, EventArgs e) { Close(); }
}

对于异常处理:

public partial class LogFileWin : Form
{
    public LogFileWin()
    {
        InitializeComponent();
    }

    private void LogFileWin_Load(object s, EventArgs e) { TopMost = true; }

    // new function to reset the the dialog to be shown again
    public void Reset() { richTextBox1.Text = string.Empty; }

    private void logfileButton_Click(object s, EventArgs e)
    {
        var path = @"C:\LogfileOperator\LogfileOperator.txt";
        try
        {
            // reading from a file can fail, so this
            // needs to be wrapped in a try catch
            using (var stream = new StreamReader(path))
            { 
                richTextBox1.Text = stream.ReadToEnd();
            }
        }
        catch (Exception ex)
        {

             var m = String.Format("Unable to read '{0}'; {1}",
                                   path, ex.Message);

             MessageBox.Show(message,
                             "File read error",
                             MessageBoxButtons.Ok,
                             MessageBoxIcon.Error);
        }
    }

    private void closeBut_Click(object s, EventArgs e) { this.Close(); }

    // we can re-use the folder browser dialog;
    // don't need to create a new one every time
    FolderBrowserDialog _ofd = new FolderBrowserDialog();

    private void usbBut_Click(object sender, EventArgs e)
    {
        var sourceFileName = "LogfileOperator.txt";
        var destFolder = @"C:\LogfileOperator";
        var destFileName = string.Empty;

        try
        {
            if (_ofd.ShowDialog() == DialogResult.OK)
            {
                destFileName = Path.Combine(_ofd.SelectedPath, 
                                            sourceFileName);

                // every time you do anything with a 
                // file, it needs to be in a try/catch
                // in case the file doesn't exist,
                // or the user doesn't have permission.
                File.Copy(sourceFileName, destFileName, true);

                MessageBox.Show("Logfile Saved", 
                                "Message", 
                                MessageBoxButtons.OK, 
                                MessageBoxIcon.Information);
            }
        }
        catch (Exception ex)
        {
             // show a message to the user informing them
             // of the error and why it occurred.
             var m = string.Format("Copy '{0}' to '{1}' failed; {2}",
                                   sourceFileName,
                                   destFileName,
                                   ex.Message); 
             MessageBox.Show(m,
                             "File copy error",
                             MessageBoxButtons.Ok,
                             MessageBoxIcon.Error);
        }
    }
}