在vb.net中使用计时器 [英] using timer in vb.net
问题描述
我正在使用vb.net创建问题记录工具.
当任何问题从其他用户分配给用户时,他都会收到通知(我在这里使用了通知图标).
请检查代码
Private Sub tmrPrfrmDBLd_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrPrfrmDBLd.Tick Try Dim sqlGetIssueRouteInfo As String = "select IssueID from routTo where visible = ''True''and currownr = " & empID.ToString Dim IssueID As String = "" Dim IssueAssignedInfo As String = "" IssueID = DataStore.ExecuteScalar(sqlGetIssueRouteInfo, sqlConnStr) If IssueID.Length <> 0 Then IssueAssignedInfo = IssueID.ToString + " has been assigned to you" nicoIssuUpdt.ShowBalloonTip(2000, "Issue Update", IssueAssignedInfo.ToString, ToolTipIcon.Info) Else Exit Sub End If Catch ex As Exception MsgBox(ex.ToString) End Try End Sub Private Sub nicoIssuUpdt_BalloonTipClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles nicoIssuUpdt.BalloonTipClosed Dim sqlGetIssueRouteInfo As String = "select IssueID from routTo where visible = ''True''and currownr = " & empID.ToString Dim getIssueRouteInfo As String = DataStore.ExecuteScalar(sqlGetIssueRouteInfo, sqlConnStr) Dim UpdtRouteto As String = "update routTo set visible = ''False'' where IssueID = ''" & getIssueRouteInfo.ToString & "''" DataStore.ExecuteNonQuery(UpdtRouteto, sqlConnStr) End Sub
这段代码运行良好,我每隔7秒对计时器进行一次滴答,它完美地逐一显示了所有问题.
问题是第一个问题来过两次,我无法找出它为什么会以这种方式运行-请提供代码帮助.
这种方法很容易失败,因为您使用计时器显示气球提示,并且在气球提示关闭事件中,您正在将Visible标志写入数据库,这是解决问题的一种非常糟糕的方法,您需要使用使用计时器来更新带有所有问题"的内存中的列表或数据表的计时器,您可以使用相同的查询并插入ExecuteScalar,也可以使用DataReader或数据适配器,linq也是很好的:cool:,因此您的计时器仅将结果存储在内存中
计时器应执行以下操作:
1-查询表格以查找问题
2-如果发现问题,则将所有问题都放在(列表,数据表或您喜欢的任何内容)中
3-调用显示列表中第一个问题的方法,该方法不应使用任何参数
4-设置tmrPrfrmDBLd.Enabled = 错误
现在在屏幕上显示气球提示
如果通过用户操作或超时关闭此气球提示,则需要执行以下操作:
1-从内存中的列表中删除问题
2-更新表以将visible设置为false =
3-如果列表中还有其他项目,则调用显示列表中第一个Issue的相同方法
4-如果列表中有 NO 个项目,则tmrPrfrmDBLd.enabled = true
这样可以为您节省许多甚至可能尚未发现的代码问题
一些注意事项:
在气球提示关闭事件中,您需要问自己:如果用户不在计算机前面,将会显示所有问题通知,并且在超时后它们将消失,而用户甚至都看不到它们,您的应用程序需要显示一个不同的任务栏图标,以指示存在等待用户看到的问题,或者某种形式的通知用户,当他/她回来时该应用程序需要他们的注意.
此代码看起来与您昨天发布的代码没有什么不同.您是否考虑过设置断点并学习使用调试器?我敢打赌,如果您这样做,您会发现它为何如此运行.
我的猜测是您的代码不会及时将其标记为false,但是如果没有代码和调试器来检查它就很难说.
Hi,
I''m creating an issue logging tool using vb.net.
When any issue is assigned to a user from another user then he gets a notification (I have used notify icon here).
Please check the code
Private Sub tmrPrfrmDBLd_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrPrfrmDBLd.Tick Try Dim sqlGetIssueRouteInfo As String = "select IssueID from routTo where visible = ''True''and currownr = " & empID.ToString Dim IssueID As String = "" Dim IssueAssignedInfo As String = "" IssueID = DataStore.ExecuteScalar(sqlGetIssueRouteInfo, sqlConnStr) If IssueID.Length <> 0 Then IssueAssignedInfo = IssueID.ToString + " has been assigned to you" nicoIssuUpdt.ShowBalloonTip(2000, "Issue Update", IssueAssignedInfo.ToString, ToolTipIcon.Info) Else Exit Sub End If Catch ex As Exception MsgBox(ex.ToString) End Try End Sub Private Sub nicoIssuUpdt_BalloonTipClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles nicoIssuUpdt.BalloonTipClosed Dim sqlGetIssueRouteInfo As String = "select IssueID from routTo where visible = ''True''and currownr = " & empID.ToString Dim getIssueRouteInfo As String = DataStore.ExecuteScalar(sqlGetIssueRouteInfo, sqlConnStr) Dim UpdtRouteto As String = "update routTo set visible = ''False'' where IssueID = ''" & getIssueRouteInfo.ToString & "''" DataStore.ExecuteNonQuery(UpdtRouteto, sqlConnStr) End Sub
This code is working fine, I''m ticking the timer at an interval of 7 sec and it shows all the issues perfectly one by one.
The problem is the first issue comes twice and I''m unble to find out why it is behaving in such a way - please help with a code.
The this approach is prone to failure simply because you are using a timer to show the balloon tip and in the balloon tip closed event you are writing the Visible flag to the database, this is a very bad way to solve your problem, you need to use the timer to update a list or a datatable in memory with all the "issues", you can use the same query and insted of a ExecuteScalar you can use a DataReader or a data adapter , linq is good too :cool: , so your timer only puts the result in memory
The timer should do the following :
1 - Query the Table to find Issues
2 - If issues are found then Put all of them in a (List, DataTable or anything you like )
3 - Call a method that shows a the first issue in the list, this method shouldn''t take any parameters
4 - settmrPrfrmDBLd.Enabled = False
now on the screen a balloon tip is shown
in the event of closing this balloon tip either by a user action or by time out , you need to do the following :
1 - Delete the issue from the List in memory
2 - Update the table to set visible = false
3 - if there are any more items in list then Call the same method that shows the first Issue in the list
4 - if there are NO items in list thentmrPrfrmDBLd.enabled = true
This should save you many many problems in your code you may haven''t even discovered yet
Some Notes:
In Balloon-tip closing event you need to ask yourself , what if the user isn''t in front of is computer, all the issue notification will be shown and they will disappear after reaching time out, without the user even seeing any of them, your application need to show a different tray icon to indicate that there are issues waiting for the user to be seen, or something of the sort to let the user know when he/she comes back that the application needs their attention.
This code doesn''t look different to what you posted yesterday. Have you considered setting a breakpoint and learning to use your debugger ? I bet if you do, you''ll find out why it behaves this way.
My guess is that your code doesn''t mark it as false in time, but it''s hard to say without having the code and a debugger to check it.
这篇关于在vb.net中使用计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!