我写了一个小程序;将日志文本写入文件,但日志文本文件正在连续写入。如果条件为真或假,我只想写一次,请帮我找一个解决方案。
Private Sub Comp1_Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comp1_Timer.Tick
ReDim VariableHandle(1)
Dim AirComp1On As Boolean
' Dim fileExists As Boolean = File.Exists(filepath)
VariableHandle(1) = TcClient.CreateVariableHandle("GVL1.bComp1Start")
AirComp1On = TcClient.ReadAny(VariableHandle(1), GetType(Boolean))
If AirComp1On = True Then
PictureBox15.Image = My.Resources.Resources.Compres_on
Using writer As New StreamWriter(filepath, True)
writer.WriteLine("SkyHab:Compressor Started " & DateAndTime.Now())
writer.Close()
End Using
Else
PictureBox15.Image = My.Resources.Resources.compressor
Using writer As New StreamWriter(filepath, True)
writer.WriteLine("SkyHab:Compressor Stopped" & DateAndTime.Now())
writer.Close()
End Using
End If
End Sub
答案 0 :(得分:0)
这是一种方法:
将静态(或全局)变量初始化为零,如果压缩器启动则将其设置为1,如果压缩器停止则将其设置为0.
Static status As Integer = 0
在tick处理程序中,仅当状态变量为零或状态已更改时才输出到日志。