Private Sub tmrTrade_Tick(sender As Object, e As EventArgs) Handles tmrTrade.Tick
tmrTrade.Enabled = False
Dim TradeCount As Integer = 0
Dim LastTrade As String = ""
'Stream File
Dim fStream As New System.IO.FileStream(OpenFileDialog1.FileName.ToString(), IO.FileMode.Open)
Dim sReader As New System.IO.StreamReader(fStream)
'Loop Through Lines
Do While sReader.Peek >= 0
TradeCount = TradeCount + 1
LastTrade = sReader.ReadLine
Loop
'to go back to an array
fStream.Close()
sReader.Close()
'Check if there is a new trade
If TradeCount = OldTradeCount Then
'Do nothing no new trades
tmrTrade.Enabled = True
Else
'Disable tmr until trade complete
tmrTrade.Enabled = False
'New Trade lets trade
'Remove date get put or call
LastTrade = Split(LastTrade, "|")(1)
OldTradeCount = TradeCount
'Make the trade
MakeTrade(LastTrade)
End If
'Save Copy
End Sub
基本上它的用途是检查一个.txt文件,当交易可用时,该文件会添加一行。这样做的是每10ms检查一次.txt文件中的行更改。
然而,我面临的问题是,如果我打开这个程序的多个版本,并打开相同的.txt文件,它会收到一个警告,说明该文件正由另一个程序使用。请不要我只需要阅读文件而不是写(metatrader正在写)。
答案 0 :(得分:1)
您需要添加访问模式和共享标志才能打开文件而不将其锁定
FileShare enumeration
FileAccess enumeration
Using fStream = new FileStream(OpenFileDialog1.FileName, _
FileMode.Open, FileAccess.Read, FileShare.Read)
Using sReader = new StreamReader(fStream )
....
End Using
End Using
我还在使用声明中附上了两个流,以确保在您使用它们时它们被正确处理