473,763 Members | 8,483 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.InvokeR equired = True, I would then have the
Control.BeginIn voke(New AsyncCallback(A ddressOf 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.EventArg s) Handles MyBase.Load
Debug.WriteLine ("Page load " &
Threading.Threa d.CurrentThread .GetHashCode)
End Sub

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

Private Sub GetDataCallBack (ByVal ar As IAsyncResult)
Try
If ar.IsCompleted Then
Dim deleg As GetDataDelegate = CType(ar.AsyncS tate,
GetDataDelegate )
Dim ds As dsYields = deleg.EndInvoke (ar)
Me.DataGrid1.Da taSource = ds.Production.D efaultView
Me.DataBind()
Debug.WriteLine ("Data loaded " &
Threading.Threa d.CurrentThread .GetHashCode)
End If
Catch ex As Exception
Tools.WriteExce ption(ex)
End Try
End Sub

' Windows.Forms application
Private Sub GetDataCallBack (ByVal ar As IAsyncResult)
Try
If ar.IsCompleted Then
If Me.Form1.Invoke Required Then
Me.Form1.BeginI nvoke(New AsyncCallback(A ddressOf
GetDataCallback ), New Object() {ar})
Else
Dim deleg As GetDataDelegate = CType(ar.AsyncS tate,
GetDataDelegate )
Dim ds As dsYields = deleg.EndInvoke (ar)
Me.DataGrid1.Da taSource = ds.Production.D efaultView
Debug.WriteLine ("Data loaded " &
Threading.Threa d.CurrentThread .GetHashCode)
End If
End If
Catch ex As Exception
Tools.WriteExce ption(ex)
End Try
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim deleg As New GetDataDelegate (AddressOf GetData)
deleg.BeginInvo ke(2005, New AsyncCallback(A ddressOf GetDataCallBack ),
deleg)
End Sub

Many thanks in advance
Jeremy
Nov 19 '05 #1
4 2071
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****@communi ty.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
AsyncCallbac k, if the Control.InvokeR equired = True, I would then have the
Control.BeginI nvoke(New AsyncCallback(A ddressOf 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.EventAr gs) Handles MyBase.Load
Debug.WriteLine ("Page load " &
Threading.Thre ad.CurrentThrea d.GetHashCode)
End Sub

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

Private Sub GetDataCallBack (ByVal ar As IAsyncResult)
Try
If ar.IsCompleted Then
Dim deleg As GetDataDelegate = CType(ar.AsyncS tate,
GetDataDelegat e)
Dim ds As dsYields = deleg.EndInvoke (ar)
Me.DataGrid1.Da taSource = ds.Production.D efaultView
Me.DataBind()
Debug.WriteLine ("Data loaded " &
Threading.Thre ad.CurrentThrea d.GetHashCode)
End If
Catch ex As Exception
Tools.WriteExce ption(ex)
End Try
End Sub

' Windows.Forms application
Private Sub GetDataCallBack (ByVal ar As IAsyncResult)
Try
If ar.IsCompleted Then
If Me.Form1.Invoke Required Then
Me.Form1.BeginI nvoke(New AsyncCallback(A ddressOf
GetDataCallbac k), New Object() {ar})
Else
Dim deleg As GetDataDelegate = CType(ar.AsyncS tate,
GetDataDelegat e)
Dim ds As dsYields = deleg.EndInvoke (ar)
Me.DataGrid1.Da taSource = ds.Production.D efaultView
Debug.WriteLine ("Data loaded " &
Threading.Thre ad.CurrentThrea d.GetHashCode)
End If
End If
Catch ex As Exception
Tools.WriteExce ption(ex)
End Try
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventAr gs) Handles Button1.Click
Dim deleg As New GetDataDelegate (AddressOf GetData)
deleg.BeginInvo ke(2005, New AsyncCallback(A ddressOf 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.begin invoke) 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.Be ginInvoke(..... )

//do other processing

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

ar.AsyncWaitHan dle.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
/dngrfTheASPNETH TTPRuntime.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/cpconControlExe cutionLifecycle .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.o detocode.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.publi c.dotnet.framew ork.aspnet
| NNTP-Posting-Host: dyn-170-234-71.myactv.net 24.170.234.71
| Lines: 1
| Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GP08.phx.gbl!TK 2MSFTNGP11.phx. gbl
| Xref: TK2MSFTNGXA01.p hx.gbl
microsoft.publi c.dotnet.framew ork.aspnet:1269 28
| X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.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****@communi ty.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
| >AsyncCallbac k, if the Control.InvokeR equired = True, I would then have
the
| >Control.BeginI nvoke(New AsyncCallback(A ddressOf 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.EventAr gs) Handles MyBase.Load
| > Debug.WriteLine ("Page load " &
| >Threading.Thre ad.CurrentThrea d.GetHashCode)
| >End Sub
| >
| > Private Function GetData(ByVal Crop As Integer) As dsYields
| > Return Me.da.GetData(C rop)
| > End Function
| >
| >Private Sub GetDataCallBack (ByVal ar As IAsyncResult)
| > Try
| > If ar.IsCompleted Then
| > Dim deleg As GetDataDelegate = CType(ar.AsyncS tate,
| >GetDataDelegat e)
| > Dim ds As dsYields = deleg.EndInvoke (ar)
| > Me.DataGrid1.Da taSource = ds.Production.D efaultView
| > Me.DataBind()
| > Debug.WriteLine ("Data loaded " &
| >Threading.Thre ad.CurrentThrea d.GetHashCode)
| > End If
| > Catch ex As Exception
| > Tools.WriteExce ption(ex)
| > End Try
| >End Sub
| >
| >' Windows.Forms application
| >Private Sub GetDataCallBack (ByVal ar As IAsyncResult)
| > Try
| > If ar.IsCompleted Then
| > If Me.Form1.Invoke Required Then
| > Me.Form1.BeginI nvoke(New AsyncCallback(A ddressOf
| >GetDataCallbac k), New Object() {ar})
| > Else
| > Dim deleg As GetDataDelegate = CType(ar.AsyncS tate,
| >GetDataDelegat e)
| > Dim ds As dsYields = deleg.EndInvoke (ar)
| > Me.DataGrid1.Da taSource = ds.Production.D efaultView
| > Debug.WriteLine ("Data loaded " &
| >Threading.Thre ad.CurrentThrea d.GetHashCode)
| > End If
| > End If
| > Catch ex As Exception
| > Tools.WriteExce ption(ex)
| > End Try
| >End Sub
| >
| >Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
| >System.EventAr gs) Handles Button1.Click
| > Dim deleg As New GetDataDelegate (AddressOf GetData)
| > deleg.BeginInvo ke(2005, New AsyncCallback(A ddressOf
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******** ******@TK2MSFTN GXA01.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.begin invoke) 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.Be ginInvoke(..... )

//do other processing

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

ar.AsyncWaitHan dle.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
/dngrfTheASPNETH TTPRuntime.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/cpconControlExe cutionLifecycle .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.o detocode.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.publi c.dotnet.framew ork.aspnet
| NNTP-Posting-Host: dyn-170-234-71.myactv.net 24.170.234.71
| Lines: 1
| Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GP08.phx.gbl!TK 2MSFTNGP11.phx. gbl
| Xref: TK2MSFTNGXA01.p hx.gbl
microsoft.publi c.dotnet.framew ork.aspnet:1269 28
| X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.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****@communi ty.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
| >AsyncCallbac k, if the Control.InvokeR equired = True, I would then have
the
| >Control.BeginI nvoke(New AsyncCallback(A ddressOf 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.EventAr gs) Handles MyBase.Load
| > Debug.WriteLine ("Page load " &
| >Threading.Thre ad.CurrentThrea d.GetHashCode)
| >End Sub
| >
| > Private Function GetData(ByVal Crop As Integer) As dsYields
| > Return Me.da.GetData(C rop)
| > End Function
| >
| >Private Sub GetDataCallBack (ByVal ar As IAsyncResult)
| > Try
| > If ar.IsCompleted Then
| > Dim deleg As GetDataDelegate = CType(ar.AsyncS tate,
| >GetDataDelegat e)
| > Dim ds As dsYields = deleg.EndInvoke (ar)
| > Me.DataGrid1.Da taSource = ds.Production.D efaultView
| > Me.DataBind()
| > Debug.WriteLine ("Data loaded " &
| >Threading.Thre ad.CurrentThrea d.GetHashCode)
| > End If
| > Catch ex As Exception
| > Tools.WriteExce ption(ex)
| > End Try
| >End Sub
| >
| >' Windows.Forms application
| >Private Sub GetDataCallBack (ByVal ar As IAsyncResult)
| > Try
| > If ar.IsCompleted Then
| > If Me.Form1.Invoke Required Then
| > Me.Form1.BeginI nvoke(New AsyncCallback(A ddressOf
| >GetDataCallbac k), New Object() {ar})
| > Else
| > Dim deleg As GetDataDelegate = CType(ar.AsyncS tate,
| >GetDataDelegat e)
| > Dim ds As dsYields = deleg.EndInvoke (ar)
| > Me.DataGrid1.Da taSource = ds.Production.D efaultView
| > Debug.WriteLine ("Data loaded " &
| >Threading.Thre ad.CurrentThrea d.GetHashCode)
| > End If
| > End If
| > Catch ex As Exception
| > Tools.WriteExce ption(ex)
| > End Try
| >End Sub
| >
| >Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
| >System.EventAr gs) Handles Button1.Click
| > Dim deleg As New GetDataDelegate (AddressOf GetData)
| > deleg.BeginInvo ke(2005, New AsyncCallback(A ddressOf
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****@communi ty.nospam>
| References: <e6************ **@TK2MSFTNGP15 .phx.gbl>
<lv************ *************** *****@4ax.com>
<pL************ **@TK2MSFTNGXA0 1.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.publi c.dotnet.framew ork.aspnet
| NNTP-Posting-Host: 201009013117.us er.veloxzone.co m.br 201.9.13.117
| Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GP08.phx.gbl!TK 2MSFTNGP10.phx. gbl
| Xref: TK2MSFTNGXA01.p hx.gbl
microsoft.publi c.dotnet.framew ork.aspnet:1272 51
| X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.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******** ******@TK2MSFTN GXA01.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.begin invoke) 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.Be ginInvoke(..... )
| >
| > //do other processing
| >
| > //after processing other task, pending under the async method finish
| >
| > ar.AsyncWaitHan dle.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
| > /dngrfTheASPNETH TTPRuntime.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/cpconControlExe cutionLifecycle .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.o detocode.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.publi c.dotnet.framew ork.aspnet
| > | NNTP-Posting-Host: dyn-170-234-71.myactv.net 24.170.234.71
| > | Lines: 1
| > | Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GP08.phx.gbl!TK 2MSFTNGP11.phx. gbl
| > | Xref: TK2MSFTNGXA01.p hx.gbl
| > microsoft.publi c.dotnet.framew ork.aspnet:1269 28
| > | X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.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****@communi ty.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
| > | >AsyncCallbac k, if the Control.InvokeR equired = True, I would then
have
| > the
| > | >Control.BeginI nvoke(New AsyncCallback(A ddressOf 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.EventAr gs) Handles MyBase.Load
| > | > Debug.WriteLine ("Page load " &
| > | >Threading.Thre ad.CurrentThrea d.GetHashCode)
| > | >End Sub
| > | >
| > | > Private Function GetData(ByVal Crop As Integer) As dsYields
| > | > Return Me.da.GetData(C rop)
| > | > End Function
| > | >
| > | >Private Sub GetDataCallBack (ByVal ar As IAsyncResult)
| > | > Try
| > | > If ar.IsCompleted Then
| > | > Dim deleg As GetDataDelegate = CType(ar.AsyncS tate,
| > | >GetDataDelegat e)
| > | > Dim ds As dsYields = deleg.EndInvoke (ar)
| > | > Me.DataGrid1.Da taSource = ds.Production.D efaultView
| > | > Me.DataBind()
| > | > Debug.WriteLine ("Data loaded " &
| > | >Threading.Thre ad.CurrentThrea d.GetHashCode)
| > | > End If
| > | > Catch ex As Exception
| > | > Tools.WriteExce ption(ex)
| > | > End Try
| > | >End Sub
| > | >
| > | >' Windows.Forms application
| > | >Private Sub GetDataCallBack (ByVal ar As IAsyncResult)
| > | > Try
| > | > If ar.IsCompleted Then
| > | > If Me.Form1.Invoke Required Then
| > | > Me.Form1.BeginI nvoke(New AsyncCallback(A ddressOf
| > | >GetDataCallbac k), New Object() {ar})
| > | > Else
| > | > Dim deleg As GetDataDelegate = CType(ar.AsyncS tate,
| > | >GetDataDelegat e)
| > | > Dim ds As dsYields = deleg.EndInvoke (ar)
| > | > Me.DataGrid1.Da taSource = ds.Production.D efaultView
| > | > Debug.WriteLine ("Data loaded " &
| > | >Threading.Thre ad.CurrentThrea d.GetHashCode)
| > | > End If
| > | > End If
| > | > Catch ex As Exception
| > | > Tools.WriteExce ption(ex)
| > | > End Try
| > | >End Sub
| > | >
| > | >Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
| > | >System.EventAr gs) Handles Button1.Click
| > | > Dim deleg As New GetDataDelegate (AddressOf GetData)
| > | > deleg.BeginInvo ke(2005, New AsyncCallback(A ddressOf
| > 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
2086
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, with a duration of 10-40 seconds. To avoid delays, i inserted a cache for the data. So, if the script is called, it returns the last caculated data-structure and then the function is called again and the new data is stored in the cache. (There is no...
2
2402
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 label to each tab page. On the first tab page add a button. When the button is pressed the code behind the button creates a thread and starts that thread. The only thing that the thread is to do is to switch from the first tab page to the 2nd tab...
5
2037
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 when I try and run another program, open a menu, or anything, I miss getting bytes. If I set the threads priority to above normal it works fine, but the rest of the program slows down dramatically. Is there any command or way to ensure that a block...
6
4094
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 appreciate if someone can tell me how to resolve this problem. Main ( Args ) { Form1 f1 = new Form1();
5
1591
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 process of changing webhosts right now..) it works pretty well, when user switches from page to page colors are
4
1462
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 (http://www.dynamicdrive.com/dynamicindex9/stylesheetswitcher.htm) but not for PAGE layouts. I mean I can make a menu to switch between a Wide Page Layout and Narrow Page Layout but I want that to be stored into a cookiee so that will show up when the visitor check back again. THANK...
1
1812
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 that i can save switching time. There is one quite similar question, but that was posted in september 2006, and there is no reply to that. here is the link for that: http://www.thescripts.com/forum/thread534922-pthread.html Thanks
1
1683
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 without most of the drawbacks (in particular, unlike real frames, this method is bookmarks-friendly and search engine-friendly). However, the method still has sufficient drawbacks from an accessibility point of view that I have been planning to...
4
2322
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 usage is - well, does it work? Can you continue browsing in your selected language? I am especially interested in people using an IE browser with a locale setting that is not english or french.
0
9564
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
9387
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
10148
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...
0
10002
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.