473,386 Members | 1,795 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,386 software developers and data experts.

Async thread not freeing up UI

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 b
Dec 11 '05 #1
11 1320
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" <ma**@discussions.microsoft.com> wrote in message
news:BB**********************************@microsof t.com...
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 b

Dec 11 '05 #2

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" wrote:
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" <ma**@discussions.microsoft.com> wrote in message
news:BB**********************************@microsof t.com...
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 b


Dec 11 '05 #3

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" <ma**@discussions.microsoft.com> schreef in bericht
news:64**********************************@microsof t.com...

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" wrote:
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" <ma**@discussions.microsoft.com> wrote in message
news:BB**********************************@microsof t.com...
> 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 b


Dec 11 '05 #4
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

mark wrote:
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 b


Dec 11 '05 #5
Mark,

The keywords you have to look at for what you are doing are

Threading.thread.sleep(milliseconds)

http://msdn2.microsoft.com/en-us/library/d00bd51t.aspx

Queue class.

http://msdn2.microsoft.com/en-us/lib...ons.queue.aspx

And for version 2005

Backgroundworker

http://msdn2.microsoft.com/en-us/library/c8dcext2.aspx

It will probably make your application a lot easier.

I hope this helps,

Cor
"mark" <ma**@discussions.microsoft.com> schreef in bericht
news:BB**********************************@microsof t.com...
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 b

Dec 12 '05 #6
Brian

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

Do you mean the Button3 control?

--
mark b
"Brian Gideon" wrote:
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

mark wrote:
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 b


Dec 15 '05 #7
Cor,

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

What do I do with Queue?
--
mark b
"Cor Ligthert [MVP]" wrote:
Mark,

The keywords you have to look at for what you are doing are

Threading.thread.sleep(milliseconds)

http://msdn2.microsoft.com/en-us/library/d00bd51t.aspx

Queue class.

http://msdn2.microsoft.com/en-us/lib...ons.queue.aspx

And for version 2005

Backgroundworker

http://msdn2.microsoft.com/en-us/library/c8dcext2.aspx

It will probably make your application a lot easier.

I hope this helps,

Cor
"mark" <ma**@discussions.microsoft.com> schreef in bericht
news:BB**********************************@microsof t.com...
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 b


Dec 15 '05 #8

mark wrote:
Brian

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

Do you mean the Button3 control?


Yes.

Dec 15 '05 #9

mark wrote:
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.

Dec 15 '05 #10
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
"mark" <ma**@discussions.microsoft.com> schreef in bericht
news:F4**********************************@microsof t.com...
Cor,

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

What do I do with Queue?
--
mark b
"Cor Ligthert [MVP]" wrote:
Mark,

The keywords you have to look at for what you are doing are

Threading.thread.sleep(milliseconds)

http://msdn2.microsoft.com/en-us/library/d00bd51t.aspx

Queue class.

http://msdn2.microsoft.com/en-us/lib...ons.queue.aspx

And for version 2005

Backgroundworker

http://msdn2.microsoft.com/en-us/library/c8dcext2.aspx

It will probably make your application a lot easier.

I hope this helps,

Cor
"mark" <ma**@discussions.microsoft.com> schreef in bericht
news:BB**********************************@microsof t.com...
> 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 b


Dec 15 '05 #11
It looks like the Control.EndInvoke method will also block the thread.
--
mark b
"Brian Gideon" wrote:

mark wrote:
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.

Dec 15 '05 #12

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

Similar topics

1
by: scott ocamb | last post by:
hello I have implemented a solution using async methods. There is one async method that can be invoked multiple times, ie there are multiple async "threads" running at a time. When these...
8
by: Dave | last post by:
I'm using the BeginInvoke method of a delegate to invoke a thread asynchronously and then use the EndInvoke to retrieve the value. This works wonderfully until a Serviced Component is added to...
6
by: Amy L. | last post by:
I am working on a project where I will have a ton of async DNS calls in a console application. I would like to process the results of the Aync calls on the same thread that made the async call. ...
5
by: Paul Hasell | last post by:
Hi, I'm trying to invoke a web method asynchronously but just can't seem to get it to tell me when it has finished! Below is the code I am (currently) using: private void...
6
by: Shak | last post by:
Hi all, Three questions really: 1) The async call to the networkstream's endread() (or even endxxx() in general) blocks. Async calls are made on the threadpool - aren't we advised not to...
7
by: Shak | last post by:
Hi all, I'm trying to write a thread-safe async method to send a message of the form (type)(contents). My model is as follows: private void SendMessage(int type, string message) { //lets...
7
by: =?Utf-8?B?Q2FybG8gRm9saW5p?= | last post by:
Hi, I implemented asynchronous calls to a web resource (using HttpWebRequest) from asp.net 2.0. The request it's made asyncronously (I see that beginGetResponse returns immediately). The number...
12
by: =?Utf-8?B?cGI=?= | last post by:
I am having trouble doing a redirect in an async asp.net implemention. Most of the time it works, but when it doesn't it just "hangs", the browser never gets any return page. If I run it under the...
3
by: Ryan Liu | last post by:
Will TcpClient.GetStream().Read()/ReadByte() block until at least one byte of data can be read? In a Client/Server application, what does it mean at the end of stream/no more data available? ...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.