472,782 Members | 1,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 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 3785
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: dixp | last post by:
I'm new to writing multithreaded apps and I have a design question. I have a winforms app and a class which has a method that does processing which is time intensive. I want the user to be able...
47
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
9
by: tommy | last post by:
hi, i have found a example for multithreading and asp.net http://www.fawcette.com/vsm/2002_11/magazine/features/chester/ i want to speed up my website ... if my website is starting, they...
2
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and...
55
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to...
5
by: sandy82 | last post by:
Whats actuallly multithreading is ... and how threading and multithreading differ . Can any1 guide how multithreading is used on the Web .. i mean a practical scenario in which u use...
2
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.