Reputation: 1422
I am trying to create a dialog box that shows the process of loading certain bill data to Word from a WPF VB.NET app. The problem is that the WPF UI is not updating to show the change.
WPF Code is:
<StackPanel Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" >
<TextBlock Name="StatusDisplayTextBlock" Text="Printing 30 of 40 to document......" FontSize="20"/>
<ProgressBar Name="PrintProgressBar1" Height="30" Width="400" Margin="0,10,0,0"/>
</StackPanel>
VB.NET Code:
Imports Microsoft.Office.Interop
Public Class PrintingToDocx
Dim IsWorkDone As Boolean = False
Public Enum BillPrintMode
InOne = 0
Seperate = 1
End Enum
Private BrowserBills As New List(Of Classes.BillsNS.BilBrowserClass)
Private Bills As New List(Of Classes.BillsNS.BillClass)
Private PrintMode As BillPrintMode
Private Sub PrintingToDocx_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
If IsWorkDone = False Then
e.Cancel = True
End If
End Sub
Public Sub New(ByVal BrowserBills As List(Of Classes.BillsNS.BilBrowserClass), ByVal Mode As BillPrintMode)
InitializeComponent()
Me.BrowserBills = BrowserBills
PrintMode = Mode
End Sub
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
GetBills()
If PrintMode = BillPrintMode.InOne Then
PrintInOne()
' IsWorkDone = True
Else
Dim count As Integer = 0
For Each k In Bills
count = count + 1
StatusDisplayTextBlock.Text = "Writing Bill " & count & " of " & Bills.Count & " to WORD....."
DataAccessModuleNS.Reports.PrintaBill(k)
Next
IsWorkDone = True
End If
Me.Close()
End Sub
Private Sub GetBills()
Me.Focus()
StatusDisplayTextBlock.Text = "Reading Bills Data...."
Dim count As Integer = 1
For Each k In BrowserBills
Bills.Add(DataAccessModuleNS.Bills.GetBill(k.ID.ToString))
StatusDisplayTextBlock.Text = "Reading Bill " & count & " of " & BrowserBills.Count & "...."
'count = count + 1
count += 1
Next
End Sub
Private Sub PrintInOne()
Try
StatusDisplayTextBlock.Text = "Writing Bill " & "0" & " of " & Bills.Count & " to WORD....."
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
Me.Focus()
'Start Word and open the document template.
oWord = CreateObject("Word.Application")
oWord.Visible = False
oDoc = oWord.Documents.Add
Dim countBills As Integer = 0
For Each Bill In Bills
'To Addressing Fields
countBills = countBills + 1
StatusDisplayTextBlock.Text = "Writing Bill " & countBills & " of " & Bills.Count & " to WORD....."
End Sub
End Class
The problem is like this:
The functionality if working fine, but the UI is not displaying until all the processing is complete. Following is the sample image of taskbar at time of processing to word:
when work is done it is working fine like this:
Following is the Report
EDIT: I now used the background worker, but now the issue is that when the progress changed and I try to update the TextBlock on UI thread I get the error that the action on a different thread cannot be done. Following is the code I used in ProgressChanged event.
Private Sub BackgroundWorker_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
'Throw New NotImplementedException
StatusDisplayTextBlock.Text = e.UserState
End Sub
Could you please help me in figuring out how to update the UI thread.
Upvotes: 0
Views: 1552
Reputation: 717
You need to move the logic operation from UI thread...
Best solution would be to use Backgroundworker class to separate all operations from the UI. Good tutorial on that can be found here [link]
If by change you can use .NET 4.5, you can simlpy use Async-Await pattern [Async in VB]
Upvotes: 0