473,770 Members | 4,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EventArg s) Handles AB.Click
Dim NoArg1 As New NoArgs(AddressO f AxB)
NoArg1.BeginInv oke(Nothing, Nothing)
End Sub

Private Sub AxB()
Dim mult As New TwoArgs(Address Of solver.Matrix_M ultiply)
Dim AsyncResult As IAsyncResult = mult.BeginInvok e(A, B, Nothing,
Nothing)
Do Until AsyncResult.IsC ompleted
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 1342
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**@discussio ns.microsoft.co m> wrote in message
news:BB******** *************** ***********@mic rosoft.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.EventArg s) Handles AB.Click
Dim NoArg1 As New NoArgs(AddressO f AxB)
NoArg1.BeginInv oke(Nothing, Nothing)
End Sub

Private Sub AxB()
Dim mult As New TwoArgs(Address Of solver.Matrix_M ultiply)
Dim AsyncResult As IAsyncResult = mult.BeginInvok e(A, B, Nothing,
Nothing)
Do Until AsyncResult.IsC ompleted
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**@discussio ns.microsoft.co m> wrote in message
news:BB******** *************** ***********@mic rosoft.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.EventArg s) Handles AB.Click
Dim NoArg1 As New NoArgs(AddressO f AxB)
NoArg1.BeginInv oke(Nothing, Nothing)
End Sub

Private Sub AxB()
Dim mult As New TwoArgs(Address Of solver.Matrix_M ultiply)
Dim AsyncResult As IAsyncResult = mult.BeginInvok e(A, B, Nothing,
Nothing)
Do Until AsyncResult.IsC ompleted
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**@discussio ns.microsoft.co m> schreef in bericht
news:64******** *************** ***********@mic rosoft.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**@discussio ns.microsoft.co m> wrote in message
news:BB******** *************** ***********@mic rosoft.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.EventArg s) Handles AB.Click
> Dim NoArg1 As New NoArgs(AddressO f AxB)
> NoArg1.BeginInv oke(Nothing, Nothing)
> End Sub
>
> Private Sub AxB()
> Dim mult As New TwoArgs(Address Of solver.Matrix_M ultiply)
> Dim AsyncResult As IAsyncResult = mult.BeginInvok e(A, B,
> Nothing,
> Nothing)
> Do Until AsyncResult.IsC ompleted
> 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.EventArg s) Handles AB.Click
Dim NoArg1 As New NoArgs(AddressO f AxB)
NoArg1.BeginInv oke(Nothing, Nothing)
End Sub

Private Sub AxB()
Dim mult As New TwoArgs(Address Of solver.Matrix_M ultiply)
Dim AsyncResult As IAsyncResult = mult.BeginInvok e(A, B, Nothing,
Nothing)
Do Until AsyncResult.IsC ompleted
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.threa d.sleep(millise conds)

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

Backgroundworke r

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**@discussio ns.microsoft.co m> schreef in bericht
news:BB******** *************** ***********@mic rosoft.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.EventArg s) Handles AB.Click
Dim NoArg1 As New NoArgs(AddressO f AxB)
NoArg1.BeginInv oke(Nothing, Nothing)
End Sub

Private Sub AxB()
Dim mult As New TwoArgs(Address Of solver.Matrix_M ultiply)
Dim AsyncResult As IAsyncResult = mult.BeginInvok e(A, B, Nothing,
Nothing)
Do Until AsyncResult.IsC ompleted
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.EventArg s) Handles AB.Click
Dim NoArg1 As New NoArgs(AddressO f AxB)
NoArg1.BeginInv oke(Nothing, Nothing)
End Sub

Private Sub AxB()
Dim mult As New TwoArgs(Address Of solver.Matrix_M ultiply)
Dim AsyncResult As IAsyncResult = mult.BeginInvok e(A, B, Nothing,
Nothing)
Do Until AsyncResult.IsC ompleted
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.IsC ompleted?

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.threa d.sleep(millise conds)

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

Backgroundworke r

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**@discussio ns.microsoft.co m> schreef in bericht
news:BB******** *************** ***********@mic rosoft.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.EventArg s) Handles AB.Click
Dim NoArg1 As New NoArgs(AddressO f AxB)
NoArg1.BeginInv oke(Nothing, Nothing)
End Sub

Private Sub AxB()
Dim mult As New TwoArgs(Address Of solver.Matrix_M ultiply)
Dim AsyncResult As IAsyncResult = mult.BeginInvok e(A, B, Nothing,
Nothing)
Do Until AsyncResult.IsC ompleted
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.IsC ompleted?
You can use the IAsyncResult.As yncWaitHandle.W aitOne 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.Is Completed returns true.

What do I do with Queue?


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

Dec 15 '05 #10

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

Similar topics

1
7140
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 threads are complete, the call the Callback method. Each "thread" calls the same callback method. What thread does this callback method exist on? My testing indicates the
8
2005
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 the mix. When the Serviced Component is invoked with BeginInvoke, the code hangs for the duration of the call rather than running async. Any ideas? Sample code below: Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As...
6
1935
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. Now I was looking at the Async WaitHandle options. They are "WaitOne", "WaitAny", and "WaitAll". I would like to process all of the results that are returned. However, due to network performance and other factors not all of the results are...
5
3257
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 btnUpload_Click(object sender, System.EventArgs e) { try { SOPWebService.Client uploader = new
6
3823
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 cause these to block? 2) You can connect together a binaryreader to a networkstream:
7
2864
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 send the messagetype via async NetworkStream ns = client.GetStream(); //assume client globally accessible
7
5079
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 of worker thread and completionPortThreads are over 300. Making 200 calls I see that 100 are queued. There's a counter telling how much async calls are handled? How can I raise the number of request handled?
12
3470
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 debugger, it works fine, though every so often I get a HttpException. System.Web.HttpException was caught ErrorCode=-2147024809 Message="An error occurred while communicating with the remote host. The error code is 0x80070057."...
3
2688
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? Client could send data once few seconds of minutes. Is there an "end" at all? In a C/S application, if server side call BeginginRead() again in EndRead() to create a endless loop to get message from client, is this a better approach than "one...
0
9602
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10017
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.