简体   繁体   English

VB - 一次显示msgbox(r)

[英]VB - show msgbox once a time(r)

I have the following code : 我有以下代码:

Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim mesaj As New Integer

        My.Computer.Network.DownloadFile("http://rotutorial.net/anunt.txt", "c:\classmate\msg1.txt", "", "", False, 60000, True)
        Dim readtext As New System.IO.StreamReader("c:\classmate\msg1.txt")
        Dim text As String
        text = readtext.ReadToEnd
        readtext.Close()
        Dim parti(10) As String
        parti = text.Split("_")

        Dim writetext1 As New System.IO.StreamReader("c:\classmate\msg.txt")
        Dim text1 As String
        Dim parti1(10) As String
        text1 = writetext1.ReadToEnd
        parti1 = text1.Split("_")

        writetext1.Close()
        Dim unic As New Integer
        unic = Val(parti(0))
        Dim unic1 As New Integer
        unic1 = Val(parti1(0))

        If unic <> unic1 Then
            If unic <> unic1 Then
                mesaj = MsgBox(parti(3), vbYesNo, "Mesaj")
            End If
            Dim writetext2 As New System.IO.StreamWriter("c:\classmate\msg.txt")
            Dim text2 As String
            text2 = text & "/" & text1
            writetext2.Write(text2)
            writetext2.Close()

            Timer1.Enabled = False
            Timer1.Enabled = True
        End If
        Timer1.Enabled = False
        Timer1.Enabled = True






    End Sub

The timer interval is set to 5000(5 seconds), but every time when the timer is ticking the msgbox appears on screen but the in the file msg.txt is writting once. 定时器间隔设置为5000(5秒),但每次定时器正在滴答时,msgbox出现在屏幕上,但文件msg.txt正在写入一次。 So, The timer check if that unic is different from unic1, and if is different shows up a msg box, and it's writting the new line in msg.txt, but on next timer tick, even if the unic and unic1 are equals the msgbox shows up anyway, but it's more interesting because it doesn't write again in file, only shows up the msgbox. 因此,计时器检查unic1是否与unic1不同,如果不同则显示一个msg框,并且它正在msg.txt中写入新行,但是在下一个计时器时,即使unic1和unic1等于msgbox无论如何都会出现,但它更有趣,因为它不会再次在文件中写入,只会显示msgbox。 I don't understand this. 我不明白这一点。

Sorry for my bad english, i'm from Romania. 抱歉我的英语不好,我来自罗马尼亚。

Thank you! 谢谢!

A message box can be a bit dangerous when used for the right reasons in the wrong place. 在错误的地方使用正确的原因时,消息框可能有点危险。 This is one of them. 这是其中之一。 The problem is that it does the equivalent of calling DoEvents, in a loop, designed to keep the message loop operational. 问题是它在循环中调用DoEvents,相当于保持消息循环可操作。 So that Windows messages, like Paint events and input events for the message box get dispatched and processed as normal. 因此,Windows消息(如Paint事件和消息框的输入事件)将被正常分派和处理。 That prevents your UI from freezing. 这可以防止您的UI冻结。

That can however cause re-entrancy problems. 然而,这可能会导致重新入侵问题。 Message box solves the most severe ones by disabling all the windows in your application. 消息框通过禁用应用程序中的所有窗口来解决最严重的问题。 Note the beep you get when you click on one of those windows. 请注意单击其中一个窗口时发出的哔声。 That however does not block Timer messages. 但是,这不会阻止定时器消息。 So after 5 seconds, your Timer1_Tick() method runs again. 所以5秒后,你的Timer1_Tick()方法再次运行。 Displaying another message box. 显示另一个消息框。 Wait long enough with dismissing them and your screen just fills up with message boxes. 等待足够长时间解雇它们,你的屏幕就会填满消息框。 You'll likely have additional problems from the file manipulation code running multiple times. 您可能会遇到多次运行的文件操作代码的其他问题。

The workaround is simple, just disable the Timer at the start of the method. 解决方法很简单,只需在方法开始时禁用Timer。 Re-enable it at the end. 最后重新启用它。 A BackgroundWorker is also a common choice in code like this, it prevents a slow file download from freezing your UI. 在这样的代码中,BackgroundWorker也是一种常见的选择,它可以防止缓慢的文件下载冻结你的UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM