试图延迟代码和不冻结申请

时间:2017-07-14 10:52:55

标签: vb.net geckofx

我使用GeckoFx浏览器并通过使窗口大于屏幕来进行手动滚动条隐藏,以便将其推出。代码和想法很有效,但我希望它现在可以实现自动化。如果我按下屏幕上的元素,请激活我的代码并检测是否显示滚动条。

问题是,我让它在GeckoWebBrowser1_DomClick之后工作,问题是我的代码在网站上的窗口/元素发生变化之前执行。

我需要代码延迟,但不会影响网页加载或影响(冻结)应用。

我尝试使用来自各地的众多答案,但没有一个能够解决我的应用程序问题。我会发布我的尝试,但遗憾的是我删除了代码并且会让我放慢速度,试图再次将它们捞出来!

如果有人可以发布一些很棒的解决方案!

我的代码:

Private Sub GeckoWebBrowser1_DomClick(sender As Object, e As Gecko.DomMouseEventArgs) Handles GeckoWebBrowser1.DomClick
        'Let page load/elements change and then do below code
        'Code here
    End Sub

2 个答案:

答案 0 :(得分:1)

尝试多线程

" Code Here"部分在一个单独的线程中,这将释放主线程冻结应用程序。

Private Sub GeckoWebBrowser1_DomClick(sender As Object, e As Gecko.DomMouseEventArgs) Handles GeckoWebBrowser1.DomClick
    'Let page load/elements change and then do below code


      browservalue = (GeckoWebBrowser1.Width - GeckoWebBrowser1.Document.Body.ScrollWidth) 
      'if you get the value here then pass it onto the following thread

     'Let's say Anything above this line is running in the Main Thread
     Dim work As New Thread(AddressOf MethodToRunAfterPageLoad) 'This will create a second thread
     work.IsBackground = True
     work.Start(browservalue)
     'this is how a single parameter is passed while threading
End Sub

Sub MethodToRunAfterPageLoad(value as Double) 'Running in second thread
    Size size = New Size(0,0)
    If value = 17 Then
        size = New Size((winwidth + 17), winheight)
    Else
        size = New Size(winwidth, winheight)
    End If

    BeginInvoke( Sub() 
                      Me.Size = size
                             End Sub )

End Sub

答案 1 :(得分:1)

尝试使用Microsoft的Reactive Framework(Rx) - 只需编辑位:“System.Reactive”和“System.Reactive.Windows.Forms”(假设您使用的是Windows Forms)。

然后你就这样做了:

Dim subscription As IDisposable = _
    Observable _
        .FromEventPattern(Of EventHandler(Of Gecko.DomMouseEventArgs), Gecko.DomMouseEventArgs)( _
            Sub(h) AddHandler GeckoWebBrowser1.DomClick, h, _
            Sub(h) RemoveHandler GeckoWebBrowser1.DomClick, h) _
        .Select(Function(x) Observable.Timer(TimeSpan.FromSeconds(1.0))) _
        .Switch() _
        .ObserveOn(Me) _
        .Subscribe(Sub(x) 
            'Code Here
        End Sub)

在执行1.0之前,这将在每个GeckoWebBrowser1.DomClick之后等待'Code Here秒。您可以轻松更改Observable.Timer(TimeSpan.FromSeconds(1.0))来电中等待的时间。

如果您想停止处理活动,只需致电subscription.Dispose()

相关问题