Multithreading problem in vb.net

  • Thread starter Multithreading problem in vb.net
  • Start date
M

Multithreading problem in vb.net

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
 
M

Multithreading problem in vb.net

Hi Ken,
Thanx for that link. I went through the article and downloaded the
sample code. It does exactly what i have been doing.
But i want this with multiple forms open as non modal without the
control being actually passed to the desktop form where the refresh
occurs.

I have found a workaround though.
Instead of passing the control from the worker thread to the UI thread
to call the databinding on the datagrid, i just get the data and store
it in a datatable. I then have another timer control on my desktop form
that databinds to that datatable. It seems to be behaving fine.

cheers!!!
Manish
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Multithreading... 1
Multithreading dilemma 9
Basic multithreading problem 2
Multithreading Invoke Workaround 3
multithreading issue 7
threading problem 5
vb.net multithreading - 5
Multithreading Question 2

Top