C#如何保存数据而不重复自身

C#如何保存数据而不重复自身,c#,C#,该程序用于显示数据,并使用pic18f4550的输入将数据存储到txt文件中。 当GUI收到的输入数据为“1”时,GUI应显示数据并将数据存储到txt文件中。可以保存数据,但如果输入仍处于“1”状态,则数据会自动重复而不停止。 如果我只想存储一次数据,即使输入仍然是“1”,我该怎么做? 这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; us

该程序用于显示数据,并使用pic18f4550的输入将数据存储到txt文件中。 当GUI收到的输入数据为“1”时,GUI应显示数据并将数据存储到txt文件中。可以保存数据,但如果输入仍处于“1”状态,则数据会自动重复而不停止。 如果我只想存储一次数据,即使输入仍然是“1”,我该怎么做? 这是我的密码:

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 Monitoring_System
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

            // Create the USB reference device object (passing VID and PID)
            theUsbDemoDevice = new usbDemoDevice(0x04D8, 0x003F);

            // Add a listener for usb events
            theUsbDemoDevice.usbEvent += new usbDemoDevice.usbEventsHandler(usbEvent_receiver);

            // Perform an initial search for the target device
            theUsbDemoDevice.findTargetDevice();

        }

        // Create an instance of the USB reference device        
        private usbDemoDevice theUsbDemoDevice;

        // Listener for USB events
        private void usbEvent_receiver(object o, EventArgs e)
        {
            // Check the status of the USB device and update the form accordingly
            if (theUsbDemoDevice.isDeviceAttached)
            {
                // Device is currently attached

                // Update the status label
                usb_status.Text = "Status : USB Device Connected";
            }
            else
            {
                // Device is currently unattached

                // Update the status label
                usb_status.Text = "Status : USB Device Unplugged";
                cr1v.Visible = false;
                cr1i.Visible = false;
                cr2v.Visible = false;
                cr2i.Visible = false;
                cr3v.Visible = false;
                cr3i.Visible = false;
                tr1v.Visible = false;
                tr1i.Visible = false;
                tr2v.Visible = false;
                tr2i.Visible = false;
                tr3v.Visible = false;
                tr3i.Visible = false;
            }
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form4 f2 = new Form4();
            f2.ShowDialog();
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {        
            if(theUsbDemoDevice.isDeviceAttached)
            {
                // Read the push button state
                bool sw1 = theUsbDemoDevice.sw1();

                if (sw1 == true)
                {
                    cr1i.Visible = false;
                    cr1v.Visible = true;
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:\Desktop\log data.txt", true))
                    {
                        file.WriteLine("Class Room 1 in use");
                        file.Close();
                    }

                }
                else if (sw1 == false)
                {
                    cr1i.Visible = true;
                    cr1v.Visible = false;
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:\Desktop\log data.txt", true))
                    {
                        file.WriteLine("Class Room 1 vacant");
                        file.Close();
                    }
                }                          
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form5 f2 = new Form5();
            f2.ShowDialog();
        }               
    }
}

看起来您正在使用计时器。计时器的每一个滴答声都会写入一个文件。您需要重新处理您的逻辑,使其不包含计时器。或者,在写入后禁用它,然后在另一个事件中重新启用它。

请提供重现您的问题的小代码示例。我们不需要转储整个应用程序,我需要转储整个应用程序来解决这个问题。