ASP.Net中Asynch处理的问题 [英] Problems with Asynch Processing in ASP.Net

查看:71
本文介绍了ASP.Net中Asynch处理的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试进行异步处理。我已经创建了一个

小测试应用程序作为概念证明,但我有一个proglem。在

示例中(下面列出的代码),我的回调例程有两个问题:


1.它运行TWICE;并且

2.虽然它似乎更新了网页控件,但结果永远不会显示在页面上。


我在MSDN上找到的几个例子和其他地方的
我使用代表。


我想要完成的是产生一个异步例程从

页面。当例程完成并且执行回调例程

时,它应该更新屏幕以让用户知道它已经完成。


任何帮助都会非常感谢。我的代码如下所示。


此致,

Glen Wolinsky


-------- ------------------

数字6:24-26

---------- ----------------

-------------------------- -------------------------------------------------- ---------------------------------


Imports System.Threading


公共类AsynchTest1


继承System.Web.UI.Page


受保护的事件btnGo As System.Web.UI.WebControls.Button

公共委托函数TestDelegate()As String

Dim intTemp as Integer


- -----页面加载

Private Sub Page_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Load
< br $>
结束子


------按钮启动过程

Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs)处理btnGo.Click

Session(" ; StartProc")= Today.Now


Dim TestDlgt As TestDelegate = New TestDelegate(AddressOf

TestRoutine)


Dim ar As IAsyncResult = TestDlgt.BeginInvoke(新
AsyncCallback(AddressOf TestComplete),_

没什么)


lblStatus。 Text ="程序开始......


结束子


-------异步例程

私有函数TestRoutine()As String

Dim start As DateTime


SyncLock Session.SyncRoot

start = Session (StartProc)

结束SyncLock


Do While(Date.Now.Ticks - start.Ticks)< 100000000

''处理此循环几秒钟

循环


返回异步过程已完成

结束函数

------完成回调例程

Private Sub TestComplete(ByVal ar as IAsyncResult)

Dim ad As TestDelegate = CType(ar.AsyncState,TestDelegate)

lblStatus.Text = ad.EndInvoke(ar)

lblStatus.ForeColor = Color.Red


End Sub


结束班

This is my first attempt as asynchronous processing. I have created a
small test app as proof of concept, but I am having one proglem. In
the example (code listed below), my callback routine has two problems:

1. It runs TWICE; and
2. While it seems to update the web page controls, the results never
show up on the page.

I am using delegates per a couple of examples I found on MSDN and
elsewhere.

What I''m trying to accomplish is to spawn an asynchronous routine from
the page. When the routine is complete and the callback routine
executes, it should update the screen to let the user know it''s done.

Any help would be greatly appreciated. My code is listed below.

Sincerely,
Glen Wolinsky

--------------------------
Numbers 6:24-26
--------------------------
-------------------------------------------------------------------------------------------------------------

Imports System.Threading

Public Class AsynchTest1

Inherits System.Web.UI.Page

Protected WithEvents btnGo As System.Web.UI.WebControls.Button
Public Delegate Function TestDelegate() As String
Dim intTemp As Integer

------Page Load
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub

------Button to start process
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGo.Click
Session("StartProc") = Today.Now

Dim TestDlgt As TestDelegate = New TestDelegate(AddressOf
TestRoutine)

Dim ar As IAsyncResult = TestDlgt.BeginInvoke(New
AsyncCallback(AddressOf TestComplete), _
Nothing)

lblStatus.Text = "Procedure started..."

End Sub

-------Asynchronous Routine
Private Function TestRoutine() As String
Dim start As DateTime

SyncLock Session.SyncRoot
start = Session("StartProc")
End SyncLock

Do While (Date.Now.Ticks - start.Ticks) < 100000000
''Process this loop for a few seconds
Loop

Return "The asynchronous process is complete"
End Function
------Completion callback routine
Private Sub TestComplete(ByVal ar As IAsyncResult)
Dim ad As TestDelegate = CType(ar.AsyncState, TestDelegate)

lblStatus.Text = ad.EndInvoke(ar)
lblStatus.ForeColor = Color.Red

End Sub

End Class

推荐答案

也许你需要使用线程。我从来没有做过任何异步的东西,但是我已经使用了很多成功的线程。


看看这个,看看是否这个帮助你:
http:// www。 fawcette.com/vsm/2002_11/...tures/chester/
http://www.sitepoint.com/article/1017/3


Michael

" Glen Wolinsky" < GW ******* @ millermartin.com>在消息中写道

news:bb ************************** @ posting.google.c om ...
Maybe you need to use Threading. I''ve never done any async stuff, but I''ve
used Threading with GREAT success.

Take a look at this and see if this helps ya:
http://www.fawcette.com/vsm/2002_11/...tures/chester/
http://www.sitepoint.com/article/1017/3

Michael
"Glen Wolinsky" <gw*******@millermartin.com> wrote in message
news:bb**************************@posting.google.c om...
这是我第一次尝试进行异步处理。我创建了一个
小测试应用程序作为概念证明,但我有一个proglem。在示例(下面列出的代码)中,我的回调例程有两个问题:

1.它运行TWICE;并且
2.虽然它似乎更新了网页控件,但结果永远不会显示在页面上。

我正在使用代表我发现的几个例子MSDN和
其他地方。

我想要完成的是从页面生成一个异步例程。当例程完成并且回调例程执行时,它应该更新屏幕以让用户知道它已经完成。

任何帮助将不胜感激。我的代码列在下面。

真诚的,
Glen Wolinsky

-------------------- ------
数字6:24-26
--------------------------
-------------------------------------------------- ------------------------
---------------------- -------------
Imports System.Threading

公共类AsynchTest1

继承System.Web.UI.Page

受保护的事件btnGo As System.Web.UI.WebControls.Button
Public Delegate Function TestDelegate()As String
Dim intTemp as Integer

--- ---页面加载
Private Sub Page_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理MyBase.Load

End Sub

------按钮启动过程
Private Sub btnGo_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理btnGo.Click
Session(" ; StartProc")= Today.Now

Dim TestDlgt As Te stDelegate = New TestDelegate(AddressOf
TestRoutine)

Dim ar As IAsyncResult = TestDlgt.BeginInvoke(New
AsyncCallback(AddressOf TestComplete),_
Nothing)

lblStatus.Text ="程序开始......

End Sub

-------异步例程
私有函数TestRoutine()As String
Dim start As DateTime

SyncLock Session.SyncRoot
start = Session(" StartProc")
End SyncLock
<执行时间(Date.Now.Ticks - start.Ticks)< 100000000
''处理此循环几秒钟
循环

返回异步过程完成
结束功能

------完成回调程序
Private Sub TestComplete(ByVal ar As IAsyncResult)
Dim ad As TestDelegate = CType(ar.AsyncState,TestDelegate)

lblStatus.Text = ad.EndInvoke(ar)
lblStatus.ForeColor = Color.Red

End Sub

End Class
This is my first attempt as asynchronous processing. I have created a
small test app as proof of concept, but I am having one proglem. In
the example (code listed below), my callback routine has two problems:

1. It runs TWICE; and
2. While it seems to update the web page controls, the results never
show up on the page.

I am using delegates per a couple of examples I found on MSDN and
elsewhere.

What I''m trying to accomplish is to spawn an asynchronous routine from
the page. When the routine is complete and the callback routine
executes, it should update the screen to let the user know it''s done.

Any help would be greatly appreciated. My code is listed below.

Sincerely,
Glen Wolinsky

--------------------------
Numbers 6:24-26
--------------------------
-------------------------------------------------------------------------- -----------------------------------
Imports System.Threading

Public Class AsynchTest1

Inherits System.Web.UI.Page

Protected WithEvents btnGo As System.Web.UI.WebControls.Button
Public Delegate Function TestDelegate() As String
Dim intTemp As Integer

------Page Load
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub

------Button to start process
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGo.Click
Session("StartProc") = Today.Now

Dim TestDlgt As TestDelegate = New TestDelegate(AddressOf
TestRoutine)

Dim ar As IAsyncResult = TestDlgt.BeginInvoke(New
AsyncCallback(AddressOf TestComplete), _
Nothing)

lblStatus.Text = "Procedure started..."

End Sub

-------Asynchronous Routine
Private Function TestRoutine() As String
Dim start As DateTime

SyncLock Session.SyncRoot
start = Session("StartProc")
End SyncLock

Do While (Date.Now.Ticks - start.Ticks) < 100000000
''Process this loop for a few seconds
Loop

Return "The asynchronous process is complete"
End Function
------Completion callback routine
Private Sub TestComplete(ByVal ar As IAsyncResult)
Dim ad As TestDelegate = CType(ar.AsyncState, TestDelegate)

lblStatus.Text = ad.EndInvoke(ar)
lblStatus.ForeColor = Color.Red

End Sub

End Class



---

外发邮件经过无病毒认证。

由AVG反病毒系统检查( http://www.grisoft.com)

版本:6.0.564 /病毒库:356 - 发布日期:1 / 19/2004


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.564 / Virus Database: 356 - Release Date: 1/19/2004


你错过了页面处理的概念。

浏览器请求页面(或点击后发布) - >


服务器构建页面

oninint

onload

onclick

onprerender

onrender

< - 页面发回


浏览器呈现页面


你在onclick期间开始异步触发,但你没有延迟(在页面上说

prerender)等待异步过程完成。因此

页面html在完成之前会被发送回浏览器。它可以更改

类变量和控件,但不会产生任何影响,因为已经发送了响应




如果你想要渲染页面,然后在异步完成后更新,

浏览器需要轮询结果。让异步例程将其

结果写入会话,然后使用刷新元标记,回发每对夫妇

秒以查看它是否已完成。一个小的javascript和一个隐藏的框架

在这里可以很方便。


- 布鲁斯(sqlwork.com)


" Glen Wolinsky" < GW ******* @ millermartin.com>在消息中写道

news:bb ************************** @ posting.google.c om ...
your missing the concept of how page processing happens.
browser requests pages (or post on click) -->

server builds page
oninint
onload
onclick
onprerender
onrender
<-- page sent back

browser renders page

you are starting a async proc during onclick, but you have no delay (say at
prerender) on the page to wait for the async process to complete. so the
page html is sent back to the browser before it completes. it can change
class variables and controls, but will have no impact, because the response
has already been sent.

if you want the page to render, then be updated after the async is done, the
browser needs to poll for the results. have the async routine write its
results to session, then using the refresh meta tag, postback every couple
seconds to see if it has completed. a little javascript and a hidden frame
can be handy here.

-- bruce (sqlwork.com)

"Glen Wolinsky" <gw*******@millermartin.com> wrote in message
news:bb**************************@posting.google.c om...
这是我第一次尝试进行异步处理。我创建了一个
小测试应用程序作为概念证明,但我有一个proglem。在示例(下面列出的代码)中,我的回调例程有两个问题:

1.它运行TWICE;并且
2.虽然它似乎更新了网页控件,但结果永远不会显示在页面上。

我正在使用代表我发现的几个例子MSDN和
其他地方。

我想要完成的是从页面生成一个异步例程。当例程完成并且回调例程执行时,它应该更新屏幕以让用户知道它已经完成。

任何帮助将不胜感激。我的代码列在下面。

真诚的,
Glen Wolinsky

-------------------- ------
数字6:24-26
--------------------------
-------------------------------------------------- ------------------------
---------------------- -------------
Imports System.Threading

公共类AsynchTest1

继承System.Web.UI.Page

受保护的事件btnGo As System.Web.UI.WebControls.Button
Public Delegate Function TestDelegate()As String
Dim intTemp as Integer

--- ---页面加载
Private Sub Page_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理MyBase.Load

End Sub

------按钮启动过程
Private Sub btnGo_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理btnGo.Click
Session(" ; StartProc")= Today.Now

Dim TestDlgt As Te stDelegate = New TestDelegate(AddressOf
TestRoutine)

Dim ar As IAsyncResult = TestDlgt.BeginInvoke(New
AsyncCallback(AddressOf TestComplete),_
Nothing)

lblStatus.Text ="程序开始......

End Sub

-------异步例程
私有函数TestRoutine()As String
Dim start As DateTime

SyncLock Session.SyncRoot
start = Session(" StartProc")
End SyncLock
<执行时间(Date.Now.Ticks - start.Ticks)< 100000000
''处理此循环几秒钟
循环

返回异步过程完成
结束功能

------完成回调程序
Private Sub TestComplete(ByVal ar As IAsyncResult)
Dim ad As TestDelegate = CType(ar.AsyncState,TestDelegate)

lblStatus.Text = ad.EndInvoke(ar)
lblStatus.ForeColor = Color.Red

End Sub

End Class
This is my first attempt as asynchronous processing. I have created a
small test app as proof of concept, but I am having one proglem. In
the example (code listed below), my callback routine has two problems:

1. It runs TWICE; and
2. While it seems to update the web page controls, the results never
show up on the page.

I am using delegates per a couple of examples I found on MSDN and
elsewhere.

What I''m trying to accomplish is to spawn an asynchronous routine from
the page. When the routine is complete and the callback routine
executes, it should update the screen to let the user know it''s done.

Any help would be greatly appreciated. My code is listed below.

Sincerely,
Glen Wolinsky

--------------------------
Numbers 6:24-26
--------------------------
-------------------------------------------------------------------------- -----------------------------------
Imports System.Threading

Public Class AsynchTest1

Inherits System.Web.UI.Page

Protected WithEvents btnGo As System.Web.UI.WebControls.Button
Public Delegate Function TestDelegate() As String
Dim intTemp As Integer

------Page Load
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub

------Button to start process
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGo.Click
Session("StartProc") = Today.Now

Dim TestDlgt As TestDelegate = New TestDelegate(AddressOf
TestRoutine)

Dim ar As IAsyncResult = TestDlgt.BeginInvoke(New
AsyncCallback(AddressOf TestComplete), _
Nothing)

lblStatus.Text = "Procedure started..."

End Sub

-------Asynchronous Routine
Private Function TestRoutine() As String
Dim start As DateTime

SyncLock Session.SyncRoot
start = Session("StartProc")
End SyncLock

Do While (Date.Now.Ticks - start.Ticks) < 100000000
''Process this loop for a few seconds
Loop

Return "The asynchronous process is complete"
End Function
------Completion callback routine
Private Sub TestComplete(ByVal ar As IAsyncResult)
Dim ad As TestDelegate = CType(ar.AsyncState, TestDelegate)

lblStatus.Text = ad.EndInvoke(ar)
lblStatus.ForeColor = Color.Red

End Sub

End Class



Michael,


感谢您的回复。我查看了线程并获得了一些样本

使用会话变量来处理状态信息。


然而,在阅读了几篇关于此类事情的文章后,

的共识似乎是asynch处理比线程更好更可靠。


我只需要这个功能少数(2-5)用户在我们的数据仓库中进行大量搜索

。搜索运行正常,但将

数据导出到其他系统或excel电子表格会导致浏览器超时



我有一个异步测试工作,但我仍然需要使用会话变量

来检查状态。


我应该使用asynch来让asp .net为我管理线程池?

任何可以就每个人的专业和反馈建议我的人都会非常感激。

此致,

Glen Wolinsky

-------------------- ------

数字6:24-26

---------------------- ----


***通过开发人员指南 http: //www.developersdex.com ***

不要只是参加USENET ......获得奖励!
Michael,

Thanks for the reply. I looked at threading and got some samples
working well using session variables for status info.

However, after reading several articles on this type of thing, the
consensus seemed to be that asynch processing was better and more
scaleable than threading.

I need this feature for just a handful (2-5) users who do large searches
on our data warehouse. The search runs fine but the exporting of that
data to other systems or to an excel spreadsheet causes the browser to
time out.

I got one asynch test working, but I still had to use session variables
to check status.

Should I be using asynch to let asp.net manage the thread pool for me?
Anyone who can advise me on the pro''s and con''s of each would be greatly
appreciated.

Sincerely,
Glen Wolinsky
--------------------------
Numbers 6:24-26
--------------------------

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


这篇关于ASP.Net中Asynch处理的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆