472,958 Members | 2,318 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,958 software developers and data experts.

Switching threads on UI in asp.net page

Hi,

In a windows.forms application I would BeginInvoke a delegate on the UI
thread to collect data from a database. When the call returns to the
AsyncCallback, if the Control.InvokeRequired = True, I would then have the
Control.BeginInvoke(New AsyncCallback(AddressOf GetDataCallback), New
Object() {ar}).

How would one achieve the same thing on an asp.net page (without using a
webseervice)? In the code below, because the GetDataCallBack returns on a
thread different to the one originally invoked, the DataGrid will not
update.

Private da As New clsYields

Private Delegate Function GetDataDelegate(ByVal Crop As Integer) As dsYields
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Debug.WriteLine("Page load " &
Threading.Thread.CurrentThread.GetHashCode)
End Sub

Private Function GetData(ByVal Crop As Integer) As dsYields
Return Me.da.GetData(Crop)
End Function

Private Sub GetDataCallBack(ByVal ar As IAsyncResult)
Try
If ar.IsCompleted Then
Dim deleg As GetDataDelegate = CType(ar.AsyncState,
GetDataDelegate)
Dim ds As dsYields = deleg.EndInvoke(ar)
Me.DataGrid1.DataSource = ds.Production.DefaultView
Me.DataBind()
Debug.WriteLine("Data loaded " &
Threading.Thread.CurrentThread.GetHashCode)
End If
Catch ex As Exception
Tools.WriteException(ex)
End Try
End Sub

' Windows.Forms application
Private Sub GetDataCallBack(ByVal ar As IAsyncResult)
Try
If ar.IsCompleted Then
If Me.Form1.InvokeRequired Then
Me.Form1.BeginInvoke(New AsyncCallback(AddressOf
GetDataCallback), New Object() {ar})
Else
Dim deleg As GetDataDelegate = CType(ar.AsyncState,
GetDataDelegate)
Dim ds As dsYields = deleg.EndInvoke(ar)
Me.DataGrid1.DataSource = ds.Production.DefaultView
Debug.WriteLine("Data loaded " &
Threading.Thread.CurrentThread.GetHashCode)
End If
End If
Catch ex As Exception
Tools.WriteException(ex)
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim deleg As New GetDataDelegate(AddressOf GetData)
deleg.BeginInvoke(2005, New AsyncCallback(AddressOf GetDataCallBack),
deleg)
End Sub

Many thanks in advance
Jeremy
Nov 19 '05 #1
4 2021
Hi Jeremy:

Web controls do not have thread affinity like the Windows UI controls.
You can touch them from any thread.

There are a host of other threading issues that crop up in ASP.NET
however.

My guess is your async callback happens after the page has finished
processing. the primary response thread has probably already asked the
web form and all the controls to render before the callback happens.

Your best bet, if you need to make just one web service call, is not
to do async processing but make a blocking call and wait on the
results. The user will have to wait just as long a if you did the
async processing.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Sun, 25 Sep 2005 10:25:59 -0300, "Jeremy Holt"
<jh****@community.nospam> wrote:
Hi,

In a windows.forms application I would BeginInvoke a delegate on the UI
thread to collect data from a database. When the call returns to the
AsyncCallback, if the Control.InvokeRequired = True, I would then have the
Control.BeginInvoke(New AsyncCallback(AddressOf GetDataCallback), New
Object() {ar}).

How would one achieve the same thing on an asp.net page (without using a
webseervice)? In the code below, because the GetDataCallBack returns on a
thread different to the one originally invoked, the DataGrid will not
update.

Private da As New clsYields

Private Delegate Function GetDataDelegate(ByVal Crop As Integer) As dsYields
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Debug.WriteLine("Page load " &
Threading.Thread.CurrentThread.GetHashCode)
End Sub

Private Function GetData(ByVal Crop As Integer) As dsYields
Return Me.da.GetData(Crop)
End Function

Private Sub GetDataCallBack(ByVal ar As IAsyncResult)
Try
If ar.IsCompleted Then
Dim deleg As GetDataDelegate = CType(ar.AsyncState,
GetDataDelegate)
Dim ds As dsYields = deleg.EndInvoke(ar)
Me.DataGrid1.DataSource = ds.Production.DefaultView
Me.DataBind()
Debug.WriteLine("Data loaded " &
Threading.Thread.CurrentThread.GetHashCode)
End If
Catch ex As Exception
Tools.WriteException(ex)
End Try
End Sub

' Windows.Forms application
Private Sub GetDataCallBack(ByVal ar As IAsyncResult)
Try
If ar.IsCompleted Then
If Me.Form1.InvokeRequired Then
Me.Form1.BeginInvoke(New AsyncCallback(AddressOf
GetDataCallback), New Object() {ar})
Else
Dim deleg As GetDataDelegate = CType(ar.AsyncState,
GetDataDelegate)
Dim ds As dsYields = deleg.EndInvoke(ar)
Me.DataGrid1.DataSource = ds.Production.DefaultView
Debug.WriteLine("Data loaded " &
Threading.Thread.CurrentThread.GetHashCode)
End If
End If
Catch ex As Exception
Tools.WriteException(ex)
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim deleg As New GetDataDelegate(AddressOf GetData)
deleg.BeginInvoke(2005, New AsyncCallback(AddressOf GetDataCallBack),
deleg)
End Sub

Many thanks in advance
Jeremy


Nov 19 '05 #2
Thanks for Scott's informative inputs.

Hi Jeremy,

As Scott has mentioned, ASP.NET web page/serverside processing has
completely different model from winform application. For winform
application, there will exist a UI thread( and generally the primary thread
in process) which will process the windows message. However, in asp.net,
there is no such UI thread, and haven't the same windows message mechanism
like winform app, all the ASP.NET request are being processed under a
worker thread which is retrieved from the server process's managed thread
pool. Also, asp.net 's page request is being processed on serverside, it'll
go through a series of pipline modules and some certain events, after that
the page's request ended and response stream is written down to clientside.
So when we do asynchronous method invoking (delegate.begininvoke) which
execute on a separate threadpool thread, we can not guarantee the method
invoking will be finished before the page's request lifecycle ended( that
will cause the async delegate's callback be called too late for update the
page's properties or controls....)

So in asp.net app, we can not simply use async method invoking like in
winform, if you do need to do this, we need to make sure that before the
request processing end, the method is finished, you can ensure this through
the following code:

In postback handler

{
IAsyncResult ar = xxxxdelegate.BeginInvoke(.....)

//do other processing

//after processing other task, pending under the async method finish

ar.AsyncWaitHandle.WaitOne();
//continue ....

}

In addition, here are some articles discussing on ASP.NET's page model and
serverside lifecycle:

#The ASP.NET HTTP Runtime
http://msdn.microsoft.com/library/de...us/dnaspp/html
/dngrfTheASPNETHTTPRuntime.asp

#The ASP.NET Page Object Model
http://msdn.microsoft.com/library/de...us/dnaspp/html
/aspnet-pageobjectmodel.asp

#Control Execution Lifecycle
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconControlExecutionLifecycle.asp
Hope helps. If anything unclear, please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: Scott Allen <sc***@nospam.odetocode.com>
| Subject: Re: Switching threads on UI in asp.net page
| Date: Sun, 25 Sep 2005 15:39:07 -0400
| Message-ID: <lv********************************@4ax.com>
| References: <e6**************@TK2MSFTNGP15.phx.gbl>
| X-Newsreader: Forte Agent 1.8/32.548
| MIME-Version: 1.0
| Content-Type: text/plain; charset=us-ascii
| Content-Transfer-Encoding: 7bit
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: dyn-170-234-71.myactv.net 24.170.234.71
| Lines: 1
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP11.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:126928
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hi Jeremy:
|
| Web controls do not have thread affinity like the Windows UI controls.
| You can touch them from any thread.
|
| There are a host of other threading issues that crop up in ASP.NET
| however.
|
| My guess is your async callback happens after the page has finished
| processing. the primary response thread has probably already asked the
| web form and all the controls to render before the callback happens.
|
| Your best bet, if you need to make just one web service call, is not
| to do async processing but make a blocking call and wait on the
| results. The user will have to wait just as long a if you did the
| async processing.
|
| --
| Scott
| http://www.OdeToCode.com/blogs/scott/
|
| On Sun, 25 Sep 2005 10:25:59 -0300, "Jeremy Holt"
| <jh****@community.nospam> wrote:
|
| >Hi,
| >
| >In a windows.forms application I would BeginInvoke a delegate on the UI
| >thread to collect data from a database. When the call returns to the
| >AsyncCallback, if the Control.InvokeRequired = True, I would then have
the
| >Control.BeginInvoke(New AsyncCallback(AddressOf GetDataCallback), New
| >Object() {ar}).
| >
| >How would one achieve the same thing on an asp.net page (without using a
| >webseervice)? In the code below, because the GetDataCallBack returns on
a
| >thread different to the one originally invoked, the DataGrid will not
| >update.
| >
| >Private da As New clsYields
| >
| >Private Delegate Function GetDataDelegate(ByVal Crop As Integer) As
dsYields
| > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
| >System.EventArgs) Handles MyBase.Load
| > Debug.WriteLine("Page load " &
| >Threading.Thread.CurrentThread.GetHashCode)
| >End Sub
| >
| > Private Function GetData(ByVal Crop As Integer) As dsYields
| > Return Me.da.GetData(Crop)
| > End Function
| >
| >Private Sub GetDataCallBack(ByVal ar As IAsyncResult)
| > Try
| > If ar.IsCompleted Then
| > Dim deleg As GetDataDelegate = CType(ar.AsyncState,
| >GetDataDelegate)
| > Dim ds As dsYields = deleg.EndInvoke(ar)
| > Me.DataGrid1.DataSource = ds.Production.DefaultView
| > Me.DataBind()
| > Debug.WriteLine("Data loaded " &
| >Threading.Thread.CurrentThread.GetHashCode)
| > End If
| > Catch ex As Exception
| > Tools.WriteException(ex)
| > End Try
| >End Sub
| >
| >' Windows.Forms application
| >Private Sub GetDataCallBack(ByVal ar As IAsyncResult)
| > Try
| > If ar.IsCompleted Then
| > If Me.Form1.InvokeRequired Then
| > Me.Form1.BeginInvoke(New AsyncCallback(AddressOf
| >GetDataCallback), New Object() {ar})
| > Else
| > Dim deleg As GetDataDelegate = CType(ar.AsyncState,
| >GetDataDelegate)
| > Dim ds As dsYields = deleg.EndInvoke(ar)
| > Me.DataGrid1.DataSource = ds.Production.DefaultView
| > Debug.WriteLine("Data loaded " &
| >Threading.Thread.CurrentThread.GetHashCode)
| > End If
| > End If
| > Catch ex As Exception
| > Tools.WriteException(ex)
| > End Try
| >End Sub
| >
| >Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| >System.EventArgs) Handles Button1.Click
| > Dim deleg As New GetDataDelegate(AddressOf GetData)
| > deleg.BeginInvoke(2005, New AsyncCallback(AddressOf
GetDataCallBack),
| >deleg)
| >End Sub
| >
| >Many thanks in advance
| >Jeremy
| >
|
|

Nov 19 '05 #3
Scott/Steve,

Thank you both very much for your advice. I really hadn't understood the
point that the page could have completed loading before the async callback
had returned - it makes sense now that you have explained it so clearly!

Therefore, to summarise, I may as well do a synchronous call to get the
data, and wait for the data before letting the page complete.

Many thanks again for all your help.

Best regards
Jeremy


"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:pL**************@TK2MSFTNGXA01.phx.gbl...
Thanks for Scott's informative inputs.

Hi Jeremy,

As Scott has mentioned, ASP.NET web page/serverside processing has
completely different model from winform application. For winform
application, there will exist a UI thread( and generally the primary
thread
in process) which will process the windows message. However, in asp.net,
there is no such UI thread, and haven't the same windows message mechanism
like winform app, all the ASP.NET request are being processed under a
worker thread which is retrieved from the server process's managed thread
pool. Also, asp.net 's page request is being processed on serverside,
it'll
go through a series of pipline modules and some certain events, after that
the page's request ended and response stream is written down to
clientside.
So when we do asynchronous method invoking (delegate.begininvoke) which
execute on a separate threadpool thread, we can not guarantee the method
invoking will be finished before the page's request lifecycle ended( that
will cause the async delegate's callback be called too late for update the
page's properties or controls....)

So in asp.net app, we can not simply use async method invoking like in
winform, if you do need to do this, we need to make sure that before the
request processing end, the method is finished, you can ensure this
through
the following code:

In postback handler

{
IAsyncResult ar = xxxxdelegate.BeginInvoke(.....)

//do other processing

//after processing other task, pending under the async method finish

ar.AsyncWaitHandle.WaitOne();
//continue ....

}

In addition, here are some articles discussing on ASP.NET's page model and
serverside lifecycle:

#The ASP.NET HTTP Runtime
http://msdn.microsoft.com/library/de...us/dnaspp/html
/dngrfTheASPNETHTTPRuntime.asp

#The ASP.NET Page Object Model
http://msdn.microsoft.com/library/de...us/dnaspp/html
/aspnet-pageobjectmodel.asp

#Control Execution Lifecycle
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconControlExecutionLifecycle.asp
Hope helps. If anything unclear, please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: Scott Allen <sc***@nospam.odetocode.com>
| Subject: Re: Switching threads on UI in asp.net page
| Date: Sun, 25 Sep 2005 15:39:07 -0400
| Message-ID: <lv********************************@4ax.com>
| References: <e6**************@TK2MSFTNGP15.phx.gbl>
| X-Newsreader: Forte Agent 1.8/32.548
| MIME-Version: 1.0
| Content-Type: text/plain; charset=us-ascii
| Content-Transfer-Encoding: 7bit
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: dyn-170-234-71.myactv.net 24.170.234.71
| Lines: 1
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP11.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:126928
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hi Jeremy:
|
| Web controls do not have thread affinity like the Windows UI controls.
| You can touch them from any thread.
|
| There are a host of other threading issues that crop up in ASP.NET
| however.
|
| My guess is your async callback happens after the page has finished
| processing. the primary response thread has probably already asked the
| web form and all the controls to render before the callback happens.
|
| Your best bet, if you need to make just one web service call, is not
| to do async processing but make a blocking call and wait on the
| results. The user will have to wait just as long a if you did the
| async processing.
|
| --
| Scott
| http://www.OdeToCode.com/blogs/scott/
|
| On Sun, 25 Sep 2005 10:25:59 -0300, "Jeremy Holt"
| <jh****@community.nospam> wrote:
|
| >Hi,
| >
| >In a windows.forms application I would BeginInvoke a delegate on the UI
| >thread to collect data from a database. When the call returns to the
| >AsyncCallback, if the Control.InvokeRequired = True, I would then have
the
| >Control.BeginInvoke(New AsyncCallback(AddressOf GetDataCallback), New
| >Object() {ar}).
| >
| >How would one achieve the same thing on an asp.net page (without using
a
| >webseervice)? In the code below, because the GetDataCallBack returns on
a
| >thread different to the one originally invoked, the DataGrid will not
| >update.
| >
| >Private da As New clsYields
| >
| >Private Delegate Function GetDataDelegate(ByVal Crop As Integer) As
dsYields
| > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
| >System.EventArgs) Handles MyBase.Load
| > Debug.WriteLine("Page load " &
| >Threading.Thread.CurrentThread.GetHashCode)
| >End Sub
| >
| > Private Function GetData(ByVal Crop As Integer) As dsYields
| > Return Me.da.GetData(Crop)
| > End Function
| >
| >Private Sub GetDataCallBack(ByVal ar As IAsyncResult)
| > Try
| > If ar.IsCompleted Then
| > Dim deleg As GetDataDelegate = CType(ar.AsyncState,
| >GetDataDelegate)
| > Dim ds As dsYields = deleg.EndInvoke(ar)
| > Me.DataGrid1.DataSource = ds.Production.DefaultView
| > Me.DataBind()
| > Debug.WriteLine("Data loaded " &
| >Threading.Thread.CurrentThread.GetHashCode)
| > End If
| > Catch ex As Exception
| > Tools.WriteException(ex)
| > End Try
| >End Sub
| >
| >' Windows.Forms application
| >Private Sub GetDataCallBack(ByVal ar As IAsyncResult)
| > Try
| > If ar.IsCompleted Then
| > If Me.Form1.InvokeRequired Then
| > Me.Form1.BeginInvoke(New AsyncCallback(AddressOf
| >GetDataCallback), New Object() {ar})
| > Else
| > Dim deleg As GetDataDelegate = CType(ar.AsyncState,
| >GetDataDelegate)
| > Dim ds As dsYields = deleg.EndInvoke(ar)
| > Me.DataGrid1.DataSource = ds.Production.DefaultView
| > Debug.WriteLine("Data loaded " &
| >Threading.Thread.CurrentThread.GetHashCode)
| > End If
| > End If
| > Catch ex As Exception
| > Tools.WriteException(ex)
| > End Try
| >End Sub
| >
| >Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| >System.EventArgs) Handles Button1.Click
| > Dim deleg As New GetDataDelegate(AddressOf GetData)
| > deleg.BeginInvoke(2005, New AsyncCallback(AddressOf
GetDataCallBack),
| >deleg)
| >End Sub
| >
| >Many thanks in advance
| >Jeremy
| >
|
|

Nov 19 '05 #4
You're welcome Jeremy,

Also, If you have interests, I'd recommend you go through those MSDN
articles I mentioned earlier which will help you have a more clear
understanding on the ASP.NET's runtime processing. Also, some other
articles over internet also provides many deep description on ASP.NET 's
internal mechanism.

Good luck!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "Jeremy Holt" <jh****@community.nospam>
| References: <e6**************@TK2MSFTNGP15.phx.gbl>
<lv********************************@4ax.com>
<pL**************@TK2MSFTNGXA01.phx.gbl>
| Subject: Re: Switching threads on UI in asp.net page
| Date: Mon, 26 Sep 2005 20:02:47 -0300
| Lines: 219
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| Message-ID: <e$**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 201009013117.user.veloxzone.com.br 201.9.13.117
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP10.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:127251
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Scott/Steve,
|
| Thank you both very much for your advice. I really hadn't understood the
| point that the page could have completed loading before the async
callback
| had returned - it makes sense now that you have explained it so clearly!
|
| Therefore, to summarise, I may as well do a synchronous call to get the
| data, and wait for the data before letting the page complete.
|
| Many thanks again for all your help.
|
| Best regards
| Jeremy
|
|
|
|
| "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| news:pL**************@TK2MSFTNGXA01.phx.gbl...
| > Thanks for Scott's informative inputs.
| >
| > Hi Jeremy,
| >
| > As Scott has mentioned, ASP.NET web page/serverside processing has
| > completely different model from winform application. For winform
| > application, there will exist a UI thread( and generally the primary
| > thread
| > in process) which will process the windows message. However, in asp.net,
| > there is no such UI thread, and haven't the same windows message
mechanism
| > like winform app, all the ASP.NET request are being processed under a
| > worker thread which is retrieved from the server process's managed
thread
| > pool. Also, asp.net 's page request is being processed on serverside,
| > it'll
| > go through a series of pipline modules and some certain events, after
that
| > the page's request ended and response stream is written down to
| > clientside.
| > So when we do asynchronous method invoking (delegate.begininvoke) which
| > execute on a separate threadpool thread, we can not guarantee the method
| > invoking will be finished before the page's request lifecycle ended(
that
| > will cause the async delegate's callback be called too late for update
the
| > page's properties or controls....)
| >
| > So in asp.net app, we can not simply use async method invoking like in
| > winform, if you do need to do this, we need to make sure that before the
| > request processing end, the method is finished, you can ensure this
| > through
| > the following code:
| >
| > In postback handler
| >
| > {
| > IAsyncResult ar = xxxxdelegate.BeginInvoke(.....)
| >
| > //do other processing
| >
| > //after processing other task, pending under the async method finish
| >
| > ar.AsyncWaitHandle.WaitOne();
| >
| >
| > //continue ....
| >
| > }
| >
| >
| >
| > In addition, here are some articles discussing on ASP.NET's page model
and
| > serverside lifecycle:
| >
| > #The ASP.NET HTTP Runtime
| >
http://msdn.microsoft.com/library/de...us/dnaspp/html
| > /dngrfTheASPNETHTTPRuntime.asp
| >
| > #The ASP.NET Page Object Model
| >
http://msdn.microsoft.com/library/de...us/dnaspp/html
| > /aspnet-pageobjectmodel.asp
| >
| > #Control Execution Lifecycle
| >
http://msdn.microsoft.com/library/de...us/cpguide/htm
| > l/cpconControlExecutionLifecycle.asp
| >
| >
| > Hope helps. If anything unclear, please feel free to post here.
| >
| > Regards,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| > --------------------
| > | From: Scott Allen <sc***@nospam.odetocode.com>
| > | Subject: Re: Switching threads on UI in asp.net page
| > | Date: Sun, 25 Sep 2005 15:39:07 -0400
| > | Message-ID: <lv********************************@4ax.com>
| > | References: <e6**************@TK2MSFTNGP15.phx.gbl>
| > | X-Newsreader: Forte Agent 1.8/32.548
| > | MIME-Version: 1.0
| > | Content-Type: text/plain; charset=us-ascii
| > | Content-Transfer-Encoding: 7bit
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: dyn-170-234-71.myactv.net 24.170.234.71
| > | Lines: 1
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP11.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:126928
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | Hi Jeremy:
| > |
| > | Web controls do not have thread affinity like the Windows UI controls.
| > | You can touch them from any thread.
| > |
| > | There are a host of other threading issues that crop up in ASP.NET
| > | however.
| > |
| > | My guess is your async callback happens after the page has finished
| > | processing. the primary response thread has probably already asked the
| > | web form and all the controls to render before the callback happens.
| > |
| > | Your best bet, if you need to make just one web service call, is not
| > | to do async processing but make a blocking call and wait on the
| > | results. The user will have to wait just as long a if you did the
| > | async processing.
| > |
| > | --
| > | Scott
| > | http://www.OdeToCode.com/blogs/scott/
| > |
| > | On Sun, 25 Sep 2005 10:25:59 -0300, "Jeremy Holt"
| > | <jh****@community.nospam> wrote:
| > |
| > | >Hi,
| > | >
| > | >In a windows.forms application I would BeginInvoke a delegate on the
UI
| > | >thread to collect data from a database. When the call returns to the
| > | >AsyncCallback, if the Control.InvokeRequired = True, I would then
have
| > the
| > | >Control.BeginInvoke(New AsyncCallback(AddressOf GetDataCallback), New
| > | >Object() {ar}).
| > | >
| > | >How would one achieve the same thing on an asp.net page (without
using
| > a
| > | >webseervice)? In the code below, because the GetDataCallBack returns
on
| > a
| > | >thread different to the one originally invoked, the DataGrid will not
| > | >update.
| > | >
| > | >Private da As New clsYields
| > | >
| > | >Private Delegate Function GetDataDelegate(ByVal Crop As Integer) As
| > dsYields
| > | > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
| > | >System.EventArgs) Handles MyBase.Load
| > | > Debug.WriteLine("Page load " &
| > | >Threading.Thread.CurrentThread.GetHashCode)
| > | >End Sub
| > | >
| > | > Private Function GetData(ByVal Crop As Integer) As dsYields
| > | > Return Me.da.GetData(Crop)
| > | > End Function
| > | >
| > | >Private Sub GetDataCallBack(ByVal ar As IAsyncResult)
| > | > Try
| > | > If ar.IsCompleted Then
| > | > Dim deleg As GetDataDelegate = CType(ar.AsyncState,
| > | >GetDataDelegate)
| > | > Dim ds As dsYields = deleg.EndInvoke(ar)
| > | > Me.DataGrid1.DataSource = ds.Production.DefaultView
| > | > Me.DataBind()
| > | > Debug.WriteLine("Data loaded " &
| > | >Threading.Thread.CurrentThread.GetHashCode)
| > | > End If
| > | > Catch ex As Exception
| > | > Tools.WriteException(ex)
| > | > End Try
| > | >End Sub
| > | >
| > | >' Windows.Forms application
| > | >Private Sub GetDataCallBack(ByVal ar As IAsyncResult)
| > | > Try
| > | > If ar.IsCompleted Then
| > | > If Me.Form1.InvokeRequired Then
| > | > Me.Form1.BeginInvoke(New AsyncCallback(AddressOf
| > | >GetDataCallback), New Object() {ar})
| > | > Else
| > | > Dim deleg As GetDataDelegate = CType(ar.AsyncState,
| > | >GetDataDelegate)
| > | > Dim ds As dsYields = deleg.EndInvoke(ar)
| > | > Me.DataGrid1.DataSource = ds.Production.DefaultView
| > | > Debug.WriteLine("Data loaded " &
| > | >Threading.Thread.CurrentThread.GetHashCode)
| > | > End If
| > | > End If
| > | > Catch ex As Exception
| > | > Tools.WriteException(ex)
| > | > End Try
| > | >End Sub
| > | >
| > | >Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| > | >System.EventArgs) Handles Button1.Click
| > | > Dim deleg As New GetDataDelegate(AddressOf GetData)
| > | > deleg.BeginInvoke(2005, New AsyncCallback(AddressOf
| > GetDataCallBack),
| > | >deleg)
| > | >End Sub
| > | >
| > | >Many thanks in advance
| > | >Jeremy
| > | >
| > |
| > |
| >
|
|
|

Nov 19 '05 #5

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

Similar topics

5
by: Ralph Sluiters | last post by:
Hi, i've got a small problem with my python-script. It is a cgi-script, which is called regulary (e.g. every 5 minutes) and returns a xml-data-structure. This script calls a very slow function,...
2
by: Johann Blake | last post by:
The following is a bug I have discovered using tab pages and threads and I am looking for a workaround. Create a new Windows Forms application and add a tab control with two tab pages. Add a...
5
by: JRB | last post by:
Hi, I'm creating a small C#program that communicates through the serial port. I have a separate thread that continuously takes in data from an external device in a while loop. The problem is that...
6
by: nadeem_far | last post by:
Hello All, I am working on a .Net desktop application and I am having problem displaying a form. I am using C# and version 1.1 of the framework. here is how the code looks likes and I will...
5
by: maya | last post by:
hi, I'm using this script to switch stylesheets dynamically (in response to user input..) http://www.dynamicdrive.com/dynamicindex9/stylesheetswitcher.htm (sorry can't show url, am in...
4
by: james00 | last post by:
Switching Page Layouts!!! Does anyone have any idea how to create a script for Switching Page Layouts. I know how to create one for Style Sheet Switcher ...
1
by: grvsinghal | last post by:
I am working on solaris OS. The I have eight dual core processors, and I am using pthreads. most of my threads will run for 5-10 hours, so I want to avoid switching of thread between processors so...
1
by: Dave Rado | last post by:
Hi A while ago I discovered a way of creating css pseudo-frames, that offer users the important benefits of real frames (i.e. the navigation remains visible when you scroll down the page), but...
4
by: edgy | last post by:
Hello, I am a beginner with PHP, and I have made a language switcher on a site that I am looking for your feedback on. First of all, the page is http://www.mankar.ca My question regarding...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.