使用application.run的替代消息循环 - 不触发事件 [英] Alternative message loop using application.run - events are not fired

查看:115
本文介绍了使用application.run的替代消息循环 - 不触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨社区,我遇到了许多问题,当我使用另一个消息泵时,我无法解决这些问题,因为事件不会被触发,直到字段被自己更改并且类没有被实例化或者没有正确运行。 />
因为我不想使用Windows.Form并希望我的应用程序或多或少地作为服务运行但不完全。我选择了Windows窗体应用程序并禁用了应用程序框架,我选择从模块开始。并且因为我的应用程序需要一个消息泵,我写的不是自己关闭,而是像没有Window的Windows窗体一样操作。我不知道是什么问题,也许Threads是冲突的,但是再次没有抛出异常。查看代码:



Hi community, i been run into a lot of problems i can't solve when i use an alternative message pump, from events not firing till fields being altered on it's own and classes not being instantiated or not running correctly.
Since i don't want to use a Windows.Form and want my App to operate more or less similarly as a service but not completely. I have selected Windows Forms Application and disabled Application Framework, i have selected to start from a Module. And because my App needs a message pump i have written to not be closed by itself, rather operate like a Windows Form without a Window. I don't know what is the problem, maybe the Threads are conflicting, but then again there is no exception thrown. See the code:

Module Module1
Sub Main
         Dim absClass As New InheritorClass()
End Sub
End Module

Class AbstractClass
 Sub New()
        Me.Start()
 End Sub

   WithEvents MainTimer As New System.Timers.Timer
    Public Event Load()

    Private Sub Start()
        System.Console.WriteLine("Application has started.") 
        MainTimer.Interval = 5000
        MainTimer.Start()
          RaiseEvent Load()
        Application.Run()
          RaiseEvent Load()
    End Sub
    Sub Close()
        System.Console.WriteLine("Application will exit.")
        Application.Exit()
    End Sub
Dim retriesLeft As Integer = 10

    Private Sub MainTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles MainTimer.Elapsed 
        If retriesLeft > 0 Then
            System.Console.WriteLine(String.Format("{0} retries remaining...", retriesLeft))
            retriesLeft = retriesLeft - 1
        Else
            ' This stops the message pump, closing your application
            'Application.Exit()
            Me.Close()
        End If

    End Sub 
End Class

Class InheritorClass : Inherits AbstractClass
  Sub New()
   MyBase.New()
  End Sub

  Private Sub Me_Load() Handles Me.Load
   MsgBox("App has loaded") ' NEVER GETS TRIGGERED
  End Sub

End Class







我已设法修改代码以使其工作g方法通过直接调用但随后某些字段会自行更改或无效等。也无法从InheritorClass中调用其他类。



我尝试了什么:



------------------------- -------------------------------------------------- -----------




I have managed to modify the code to make it work using methods by directly calling but then some of the fields get altered or nullified on their own etc. Also calling other Classes from the InheritorClass is not possible.

What I have tried:

--------------------------------------------------------------------------------------

推荐答案

做一点 Google Search research [ ^ ],可以实现您想要的效果,但方法略有不同。请阅读 StackOverflow线程 [ ^ ]。它应该具有大部分(如果不是全部)你正在寻找的答案。
Doing a little Google Search research[^], it is possible to achieve what you want but the approach is slightly different. Have a read of this StackOverflow thread[^]. It should have most, if not all the answers that you are and will be looking for.


这篇关于使用application.run的替代消息循环 - 不触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆