Greetings,
I am new to multithreading and I am trying to implement it in my app.
This application is distributed application which needs to refresh
every say 5 secs to show some activities in the datagrid.
I have implemented querying the database in a separate thread and and
then showing it in the datagrid in the UI thread. It all works fine and
the datagrid gets updated every 5 secs. This happens in the desktop
(Main form) of the application.
The problem arises when i open another form as a NonModalForm (i'e
using the form.show method) from the desktop. The non modal form is
displayed and then because the desktop gets refreshed, this non modal
form gets hidden and the desktop gets the focus.
My question is, How do i prevent the desktop from getting the focus
when the non modal form has been displayed refresh occurs in the
background desktop form.
It works fine for modal forms.
cheers!!!
Manish
P.S: i have tried to call the form.show event of the nonmodal form as
soon as it gets activated but then there is a flicker every 5 secs....
which aint good.
Code:
Dim _RefreshThread As Thread
Dim _RefreshThreadStart As New ThreadStart(AddressOf
Me.RefreshWorkListItems)
Dim CallDataBindToDataGrid As New MethodInvoker(AddressOf
Me.DataBindToDataGrid)
Private Sub DesktopForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadRefreshThread()
End Sub
Private Sub LoadRefreshThread()
'creates a separate thread to load the worklist items
'in the bacckground
_RefreshThread = New Thread(_RefreshThreadStart)
_RefreshThread.IsBackground = True
_RefreshThread.Name = "RefreshThread"
_RefreshThread.Start()
End Sub
Private Sub RefreshWorkListItems()
' Sub routine used by the background thread to query database
'this method is called by the worker thread in the background
'and keeps looping
Try
ReLoop:
'gets data from database and saves in
'datatable _K2Controller.dtWorkList
_K2Controller.GetWorkList(_objUserInfo.UserID, True)
Me.BeginInvoke(CallDataBindToDataGrid)
'waits for 5 secs and then loops again
Thread.Sleep(5000)
GoTo ReLoop
Catch ex As Exception
HandleException(ex)
End Try
End Sub
Private Sub DataBindToDataGrid()
' Sub routine that is to be executed on Form's thread.
'databinds the datagird
dgWorkList.MainView = dgWorkListViewCurrentResource
dgWorkList.DataSource = _K2Controller.dtWorkList
End Sub