473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

please help on multithreading

Sam
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 call operations on Controls from a
delegate, otherwise it does not work. However each time I've done an
operation, I must update the progressbar and progresslabel, but this
cannot be done in the delegate as it does not work.

In the sample I've just done a loop to increase the progressbar, but
obviously in reality the progressbar updates and the operations on
controls (tabcontro, datagrid..) should be nested, hence the issue!

Please have a look at the sample for a clearer idea of what i'm trying
to achieve.

Thanks for your help:)

Nov 23 '05
55 3244
"Sam" <re************@hotmail.co.uk> schrieb
Armin,
What does IMO stand for ?:-)

In my opinion.
Armin
Nov 24 '05 #51
Sam wrote:
Branco,
First, thank you so much for taking the time to help me, I really
appreaciate that.
I've read your post several times, it took me some time to figure out
what's going on!
I've tried to implement your approach in my sample and I've come across
a number of issues:
The first one is that in Progress_FormClosing, the test :
If Worker.IsBusy Then
fails as Worker is always NULL at that point, and I don't know why.

The second issue I have is that the progressform doesn't show at all.

I've uploaded my sample, maybe you can have a quick look at it. It is
rather close to the code you provided me with, so it should be easy for
you to understand it. I'd be very grateful if you could tell me where
I'm doing wrong in trying to use your code.
http://graphicsxp.free.fr/WindowsApplication11.zip

With regards,


Sam,

First, sorry for the confusion. My last two posts suggest two sepparate
solutions, but it didn't become clear that the last post was
introducing a new approach, not complementing the previous one.

I noticed the confusion by checking your implementation, where both
solutions are attempted... :-D

Summarizing:

Solution 1, with multithreading:
You implement a base AsyncWork class which must be inherited by the
actual workers. The Progress form launches the work with a call to the
worker's Work method, just before going modal. This approach is more
complicated because of the issues related to multithreading (such as
the UI updating) and the extra classes, etc, but would give you a solid
framework that would Just Work (tm) as soon as you resolved these basic
issues. I'd use this solution if there's not much UI updating while
executing the concurrent work, or this UI updating can be modularized
in some way.

Solution 2, without multithreading
Every child form has a DoWork method (just as you were implementing).
As in the previous solution, you rely in the Progress form to start the
work, but since this doesn't use multithreading, you must take
additional steps such as using the progress form's Activate event (see
the code bellow). Likewise, the child form's DoWork method must rely in
Application.DoEvents to keep the UI updated. This approach seems more
ad hoc, but I'd dare to use it if most of the work consisted of UI
updates that could not be dettached from the actual background work,
which seems to be your case.

In your sample both solutions are half way implemented, I guess that's
why you had problems...

To implement Solution 2, which seems to adapt more seemlessly to your
original approach, this is what your classes would look like (notice
that I changed the order of the tests in the progress form Activate
event -- :-P ):

'In the progress form
Public Shared ReadOnly DefaultInstance As New frmProgress

Public Delegate Sub DoWorkDelegate()

Private mDone As Boolean
Private mWorker As DoWorkDelegate

Public Sub Progress_Activate( _
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Activated
If Not mWorker Is Nothing Then
If Not mDone Then
mDone = True
Application.DoEvents()
mWorker()
Close()
End If
End If
End Sub

Public Sub DoWork(ByVal Worker As DoWorkDelegate)
mDone = False
mWorker = Worker
ShowDialog()
End Sub

Public Sub UpdateProgress( _
ByVal Text As String, _
ByVal Percent As Integer)
ProgressBar1.Value = Percent
Label1.Text = Text
Label1.Refresh()
End Sub

Public Sub InitProgress( _
ByVal Text As String, _
ByVal Percent As Integer)
ProgressBar1.Minimum = 0
ProgressBar1.Value = 0
ProgressBar1.Maximum = Percent

Label1.Text = Text
Label1.Refresh()
End Sub
Also, replace the child's form code with this sample:

'In Child Form
Sub DoWork()
frmProgress.DefaultInstance.InitProgress("", 100)
For i As Integer = 0 To 100
frmProgress.DefaultInstance.UpdateProgress(i.ToStr ing & "%", i)
Label1.Text = i.ToString
Application.DoEvents()
Next
Dim dt As String() = {"test", "test1", "test2"}
DataGrid2.DataSource = dt
TabControl1.SelectedTab = TabPage2
Application.DoEvents()
End Sub

Private Sub Form1_Load( _
ByVal sender As Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load
Show()
Dim Work As frmProgress.DoWorkDelegate
Work = AddressOf DoWork
frmProgress.DefaultInstance.DoWork(Work)
End Sub

Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Button1.Click
Dim Work As frmProgress.DoWorkDelegate
Work = AddressOf DoWork
frmProgress.DefaultInstance.DoWork(Work)
End Sub

Finally, disable the TopMost property of the progress form (I don't
know why, but it was preventing the form from showing).

HTH.

Regards,

Branco.

Nov 24 '05 #52
Sam
Branco,
Thank you for this reply, it makes sense now :-)
To give you a feedback on the results:

1. It works just fine with my sample. The UI is updated correctely, the
progression is shown properly in the progressForm. Also the user cannot
click anywhere on the childform/MDI parent until the progression is
complete, which is what I want.

2. In my real application (the big one :-)), there is an issue as
during the progression, I can click on my MDI parent, which reacts to
the click ! For example, I open my child forms from a Treeview. While
one form is loading and the progression is shown in the progressForm, I
can click on another node of my tree, which leads to issues. I don't
understand how this is possible, as the progressform is shown modally !

Also, I've put Application.DoEvents() into UpdateProgress ( ), but I
guess this is no big deal, is it?

Do you have any idea on why this is happening ?

Thank you so much again
Regards,

Nov 25 '05 #53
Sam
I've found out what's wrong. VS2003 is the problem. I've reproduced the
same behaviour with the sample under VS2003. During the progression if
I click on the childform, it is responsive, whereas under VS2005 it is
not.
This is something that I can't explain... I guess this is not directely
related to this post and I should do another post on this forum
regarding this issue. Unless one of you has the answer ;-) ?

Nov 25 '05 #54
Sam
Wooooo I'm so close to get it exactely as I wanted at the beginning :-(
I'm just stuck with this one last issue regarding the Form not being
modal under VS2003...
Can anyone help please ?

Regards;

Nov 28 '05 #55
Sam
Right, just to say that I've moved to Visual studio 2005 (had to do it
anyway) so my issue is solved. Huge thanks to you guys for helping me
with this, it works great now.

Dec 7 '05 #56

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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
6
by: Michael C | last post by:
Hello Can someone please tell me what I'm doing wrong? I'm writing an application that should be using callbacks to perform asynchronous calls to the Win32 API. Problem is it never reaches my...
9
by: Popoxinhxan | last post by:
Dear experts, i want to develop an client application that consume the google search web service. In my MainForm i have a method to retrieve all the search result e.g. GetGoogleResults(). Now i...
2
by: shonend | last post by:
**** sorry about the length of the message. If you can't read the whole thing and still willing to help, read the last 2 paragraphs where the main problem is described. The introduction story is...
5
by: Boni | last post by:
Dear all, picturebox mouse move seems to start new thread each time it is called. At least I see in debuger many threads. ---Non user code-- Picturebox.MouseMove This seems to spoil my...
6
by: MeowCow | last post by:
I will try and make my question with out being too long winded. I have been doing a lot of reading on how to do multithreading and I have implemented the code from the following example on...
5
by: mrkbrndck | last post by:
Please see the code below as I am trying to use multithreading for copying files to a new location in a way that improves performance of the client windows application. The problem occurs when 2...
1
by: James | last post by:
Hi All, Just going into the world of Multithreading and have a few questions about using class's in them. If I create a calls in the main thread then start a new thread that calls another...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.