471,075 Members | 1,045 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

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

Nov 21 '05 #1
2 3705
Hi,

http://msdn.microsoft.com/msdnmag/is...s/default.aspx

Ken
-------------
"Multithreading problem in vb.net" <ma*********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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

Nov 21 '05 #2
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

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by dixp | last post: by
47 posts views Thread by mihai | last post: by
16 posts views Thread by Robert Zurer | last post: by
5 posts views Thread by sarge | last post: by
9 posts views Thread by tommy | last post: by
2 posts views Thread by Rich | last post: by
55 posts views Thread by Sam | last post: by
5 posts views Thread by sandy82 | last post: by
2 posts views Thread by Pradnya Patil | last post: by
7 posts views Thread by Ray | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.