473,394 Members | 1,875 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,394 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 2043
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...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.