How do i load the Main form of a WPF so that a seperate thread goes and gets data from the database while the form is in an apartmentstate ? (drag-able / movable)
I Know this is possible with opening a new window from your main form like this :
Private Sub openOrderWindow()
Dim OrderWindow As Orders = New Orders
OrderWindow.ShowDialog()
End Sub
Private Sub ButtonImport_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles ButtonImport.Click
Dim iThread As System.Threading.Thread = New System.Threading.Thread(AddressOf openOrderWindow)
iThread.SetApartmentState(System.Threading.ApartmentState.STA)
iThread.Start()
ListViewOrderDetail.Focus()
' Me.Close()
End Sub
because i use ‘ShowDialog’
But on the main window i can’t call ShowDialog
What i have Tried:
Private Sub AccessDBFilling()
ListViewDataGrid.ItemsSource = Nothing
accessdblist = Nothing
accessdblist = accessdb.GetFromAccess()
ListViewDataGrid.ItemsSource = accessdblist
End Sub
Private Sub refresh()
ListViewDataGrid.Dispatcher.Invoke(New Action(AddressOf AccessDBFilling))
End Sub
Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
iThread = New System.Threading.Thread(AddressOf refresh)
iThread.SetApartmentState(System.Threading.ApartmentState.STA)
iThread.Start()
End Sub
I’ve tried putting MyBase.ShowDialog() and .Show() and .9000+ other things
The only benefit I’ve got out of using a seperate thread to load from the databse is i can see the screen right away (as oppose to it being a transparent box until it loads) but i cannot move it around or minimize it
is there any way to make it Movable while it loads?
Yes. You should understand what you did.
The dispatcher of the UI Controls is correspond to the UI thread. so the UI is freezed, because you load data exactly on UI thread.
Here what you should to do:
As I’m don’t know VB, you should translate it from C# 🙂