2016-04-17 26 views
-1

我在我的应用程序有问题。我有2个winform并从窗体1调用函数到表单2中的函数来打印web浏览器,但是工作量很大。 这里我的代码:vb.net使用System.Threading.Thread打电话打印网络浏览器dost工作

形式1:

Dim th As System.Threading.Thread = New Threading.Thread(AddressOf Task_A) 
th.SetApartmentState(ApartmentState.STA) 
th.Start() 

Public Sub Task_A 
    Call form2.fishsefaresh() 
End Sub 

窗口2:

Public Sub fishsefaresh() 
Dim fac As String = " HTML CODE " 
Dim FILE_NAME As String = "my_app.html" 
Dim objWriter As New System.IO.StreamWriter(FILE_NAME) 
objWriter.Write(fac) 
objWriter.Close() 

Dim we As WebBrowser = Form2.WebBrowser1 
we.Navigate("file:///" & IO.Path.GetFullPath(".\my_app.html") 

While we.ReadyState <> WebBrowserReadyState.Complete 
Application.DoEvents() 
End While 

we.Print() 

当我运行的应用程序没有什么happend(我设置在我的电脑打印机divise和inestall),我的事情网页浏览器有使用System.Threading时出现问题。

回答

0

使用Application.DoEvents()是不是一个好主意,我不明白为什么你甚至会做到这一点时,你已经在后台线程?

我建议你做的是删除循环调用DoEvents(),而是订阅WebBrowser的DocumentCompleted事件。你可以再次取消订阅,这会导致它只会被提升一次。

更换这一切:

Dim we As WebBrowser = Form2.WebBrowser1 
we.Navigate("file:///" & IO.Path.GetFullPath(".\my_app.html") 

While we.ReadyState <> WebBrowserReadyState.Complete 
Application.DoEvents() 
End While 

we.Print() 

有了这个:

Dim we As WebBrowser = Form2.WebBrowser1 

AddHandler we.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted 

we.Navigate("file:///" & IO.Path.GetFullPath(".\my_app.html") 

然后声明事件订阅方法:

Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs) 
    Dim wb As WebBrowser = DirectCast(sender, WebBrowser) 'Cast the control that raised the event to a WebBrowser. 

    wb.Print() 'Print the page. 
    RemoveHandler wb.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted 'Remove the event subscription. 
End Sub 

希望这有助于!