Async thread not freeing up UI

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Please consider the following thread setup intended to free-up the UI while a
huge process takes place:

(declare delegates) (A B and C all global)

Private Sub AB_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles AB.Click
Dim NoArg1 As New NoArgs(AddressOf AxB)
NoArg1.BeginInvoke(Nothing, Nothing)
End Sub

Private Sub AxB()
Dim mult As New TwoArgs(AddressOf solver.Matrix_Multiply)
Dim AsyncResult As IAsyncResult = mult.BeginInvoke(A, B, Nothing,
Nothing)
Do Until AsyncResult.IsCompleted
For i = 1 To 1000000 : Next : Button3.Text = Rnd().ToString
Loop
C = mult.EndInvoke(AsyncResult)
do other stuff with C
End Sub

Matrix_Multiply is the class solver and consumes 50% of CPU resources with a
hyperthreading P4. It is floating point arithmatic intensive. When I run the
program the UI is unavailable until the final C result - which is double(,) -
is obtained. Button3.text initially shows activity but then stops. The
threading logic works fine with a local function that simply runs a loop then
returns a string.

What's wrong? How fix?
 
Hello Mark ,,,

This aproach will only work on a MP system , you could possibly acomplish
what you want ( responsive gui ) by using threading and create the worker
threads as lower prio background threads

Ofcourse will this result in longer processing time ( takes longer before
you app is finished )

regards

Michel Posseth [MCP]
 
The acronym MP is multiprocessor? If so, do you think dual core processors
can do it. I have considered the posibility that memory access is the problem
as my Matrix_multiply sub is multiplying arrays.

--
mark b


m.posseth said:
Hello Mark ,,,

This aproach will only work on a MP system , you could possibly acomplish
what you want ( responsive gui ) by using threading and create the worker
threads as lower prio background threads

Ofcourse will this result in longer processing time ( takes longer before
you app is finished )

regards

Michel Posseth [MCP]



mark said:
Please consider the following thread setup intended to free-up the UI
while a
huge process takes place:

(declare delegates) (A B and C all global)

Private Sub AB_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles AB.Click
Dim NoArg1 As New NoArgs(AddressOf AxB)
NoArg1.BeginInvoke(Nothing, Nothing)
End Sub

Private Sub AxB()
Dim mult As New TwoArgs(AddressOf solver.Matrix_Multiply)
Dim AsyncResult As IAsyncResult = mult.BeginInvoke(A, B, Nothing,
Nothing)
Do Until AsyncResult.IsCompleted
For i = 1 To 1000000 : Next : Button3.Text = Rnd().ToString
Loop
C = mult.EndInvoke(AsyncResult)
do other stuff with C
End Sub

Matrix_Multiply is the class solver and consumes 50% of CPU resources with
a
hyperthreading P4. It is floating point arithmatic intensive. When I run
the
program the UI is unavailable until the final C result - which is
double(,) -
is obtained. Button3.text initially shows activity but then stops. The
threading logic works fine with a local function that simply runs a loop
then
returns a string.

What's wrong? How fix?
 
The answer to both is yes ,

MP is multi processor system , and yes the multicore architecture that AMD
and Intel are now shipping ( where in my opinion the AMD implemetation is
far superior ) would perform as a true MP system and so solve this
bottleneck problem .

Although if it is the responsiveness you want then using the background
thread option might still be a good idea ( although the processing time
might take a lot longer )


regards

Michel Posseth [MCP]

mark said:
The acronym MP is multiprocessor? If so, do you think dual core processors
can do it. I have considered the posibility that memory access is the
problem
as my Matrix_multiply sub is multiplying arrays.

--
mark b


m.posseth said:
Hello Mark ,,,

This aproach will only work on a MP system , you could possibly
acomplish
what you want ( responsive gui ) by using threading and create the worker
threads as lower prio background threads

Ofcourse will this result in longer processing time ( takes longer before
you app is finished )

regards

Michel Posseth [MCP]



mark said:
Please consider the following thread setup intended to free-up the UI
while a
huge process takes place:

(declare delegates) (A B and C all global)

Private Sub AB_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles AB.Click
Dim NoArg1 As New NoArgs(AddressOf AxB)
NoArg1.BeginInvoke(Nothing, Nothing)
End Sub

Private Sub AxB()
Dim mult As New TwoArgs(AddressOf solver.Matrix_Multiply)
Dim AsyncResult As IAsyncResult = mult.BeginInvoke(A, B,
Nothing,
Nothing)
Do Until AsyncResult.IsCompleted
For i = 1 To 1000000 : Next : Button3.Text = Rnd().ToString
Loop
C = mult.EndInvoke(AsyncResult)
do other stuff with C
End Sub

Matrix_Multiply is the class solver and consumes 50% of CPU resources
with
a
hyperthreading P4. It is floating point arithmatic intensive. When I
run
the
program the UI is unavailable until the final C result - which is
double(,) -
is obtained. Button3.text initially shows activity but then stops. The
threading logic works fine with a local function that simply runs a
loop
then
returns a string.

What's wrong? How fix?
 
Mark,

The first thing I noticed was that you're accessing a Control on a
thread other than the one it was created on. This will cause
unpredictable problems on the 1.1 framework and an exception on the 2.0
framework. You need to use Control.Invoke to marshal the execution of
a delegate onto the UI thread so that you can access controls and forms
safely within that delegate.

Another thing I noticed is that you're spinning in a loop waiting for
the Matrix_Multiply method to complete. That will certainly consume a
lot of CPU time and make the UI somewhat unresponsive.

However, it doesn't appear that the UI would be completely unresponsive
since the UI thread isn't blocked by any of the code you posted.

Brian
 
Brian

Re: accessing a Control on a thread other than the one it was created on

Do you mean the Button3 control?
 
Cor,

Do I use the sleep function in the do loop to wait for
asyncresult.IsCompleted?

What do I do with Queue?
 
mark said:
Cor,

Do I use the sleep function in the do loop to wait for
asyncresult.IsCompleted?

You can use the IAsyncResult.AsyncWaitHandle.WaitOne method to block
the current thread until the asynchronous operation is complete. If
you want the current thread to be doing something then yes, you can use
the Thread.Sleep method to yield CPU time to other threads until
IAsyncResult.IsCompleted returns true.
What do I do with Queue?

I'm not seeing how the Queue helps in your situation either.
 
Mark,

The Queu is the most easy collection class to pass information in
assynchronous situations.

You fill it at the bottom in thread B, C, etc and you get what is in the
queu (can be more) from the top in Thread A

Your other question in your reply is already answered by Brian,

I hope this gives an idea

Cor
 
Back
Top