带有计时器和 Skype API 的控制台应用程序
所以我有一个问题,当尝试在计时器上调用 Skype API 时,它什么也没得到,我只是想循环读取消息并将它们显示在我的控制台中,但在计时器滴答时,什么也没有发生。但如果我将计时器滴答代码放在 main 中,它就可以正常工作。这是我的代码:
Imports SKYPE4COMLib
Imports System.Timers.Timer
Module main
Dim oSkype As New SKYPE4COMLib.Skype
Dim aUser = oSkype.User("echo123")
Dim aChat = oSkype.Messages(aUser.Handle)
Dim tmr As New Timers.Timer
Sub Main()
tmr.AutoReset = True
tmr.Interval = 1000
AddHandler tmr.Elapsed, AddressOf tmrTick
tmr.Enabled = True
Console.ReadLine()
End Sub
Public Sub tmrTick(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
For Each aMessages In aChat
Console.WriteLine(aMessages.FromHandle & ": " & aMessages.Body)
Next
Console.WriteLine("")
End Sub
End Module
谢谢, 亚当
so i have a problem, when trying to call the Skype API on a timer it gets nothing, im just trying to read messages on a loop and display them in my console, but on timer tick, nothing happens. But if i put the timer tick code in main it works fine. Here is my code:
Imports SKYPE4COMLib
Imports System.Timers.Timer
Module main
Dim oSkype As New SKYPE4COMLib.Skype
Dim aUser = oSkype.User("echo123")
Dim aChat = oSkype.Messages(aUser.Handle)
Dim tmr As New Timers.Timer
Sub Main()
tmr.AutoReset = True
tmr.Interval = 1000
AddHandler tmr.Elapsed, AddressOf tmrTick
tmr.Enabled = True
Console.ReadLine()
End Sub
Public Sub tmrTick(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
For Each aMessages In aChat
Console.WriteLine(aMessages.FromHandle & ": " & aMessages.Body)
Next
Console.WriteLine("")
End Sub
End Module
Thanks,
Adam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
计时器吞下异常,您可能会在计时器代码中遇到异常,很可能是跨线程异常。尝试设置 Visual Studio 以中断异常(调试>异常),看看是否得到任何结果。
如果确实遇到跨线程异常,您将无法使用 System.timer 计时器,因为它们在线程池上运行。在这种情况下,您应该使用 ui 友好的计时器,例如 winforms/wpf 中的计时器,或专用线程
Timers swallow exceptions, you might be getting an exception in the timer code, most likely a cross thread exception. Try settings visual studio to break on exceptions (debug > exceptions) and see if you get anything.
If you do get a cross thread exception, you wont be able to use System.timer timers as they run on the thread pool. in that case you should use a ui friendly timer such as the ones from winforms/wpf, or a dedicated thread