ASP.Net中的安全多线程 [英] Safe multithreading in ASP.Net

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

问题描述

考虑以下代码,在ASP.Net中启用多线程

应用程序我正在写:


global.asx中的代码Application_start子例程, Threadnotify被声明

全球作为一个新线程,它使用下面的EmailNotify地址:


ThreadNotify.IsBackground = True


ThreadNotify.Start()


--------


Public Sub EmailNotify()


昏暗的电子邮件作为新的ForumEmail


Do While ContinueNotify''布尔值设置为True所以循环将继续


emails.EmailNotify()


Thread.Sleep(notifyinterval * 60000)''notifyinterval是通过app设置的。

web.config文件中的设置


循环


结束子


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


来自Application_End子程序中的global.asx文件


ThreadNotify.Abort()

你可能告诉我们,当

应用程序启动时,Threadnotify会启动。子程序只是循环遍历并调用

子程序,然后以分钟为间隔暂停。好消息:这个有效。

坏消息:这个效果太好了。问题是,这个帖子什么时候终止?
终止了?我可以将循环中的布尔值通知设置为false,这样线程

将到达它的代码的末尾,它将终止。但是,除此之外我还有担心。我正在我的机器上编写一个应用程序并进行测试。

除非我在b / b
中将布尔值设置为false,否则我无法停止该线程循环哪个坏。当我结束应用程序的调试执行时,

线程继续,它不会停止。哎呀,我可以停止IIS服务并且线程仍然继续执行
。我怎么知道它正在执行。这个帖子的

目标是发送电子邮件。我收到的电子邮件表明哪个

告诉我这个帖子还在运行。


这会导致更大的问题。当我为其他人发布这个应用程序使用
时,即使用户

可能会结束Web服务,我怎样才能确保这个线程不会继续运行?这个傻瓜一直在继续前进。


假设我将其上传到生产网络服务器。线程启动并且

执行正常。假设我对应用程序代码进行了一些更改,并将必要的文件复制到生产Web服务器。来自

之前版本的网站的线程是否会继续,而新的线程是由

启动更新的代码,即两个线程会做同样的事情吗? />

这个线程运行的方式,我相信我可以停止web服务并销毁web应用程序的b $ b。线程仍然会运行。这是不可接受的。

如何确保此线程停止?


这个线程运行的是什么?我本以为线程会在IIS下运行

,并认为IIS是父进程。但是,我是终止IIS的
,这个线程仍在继续。什么运行

下?


最终,什么时候这个线程终止了?!我唯一能够停止这个线程的b $ b来重启机器!我很害怕将这个

发布到我的网站,因为它可能会启动并且永远不会停止,直到他们重启他们的

机器。


这个帖子应该在用户确定暂停后发送电子邮件。

还有其他办法吗?我虽然使用了一个计时器,当X

的分钟数通过时,事件就会发生。我可以捕获

事件并执行电子邮件代码。我不知道怎么做。

建议?


谢谢


Chris Smith

解决方案

几个问题。


ThreadNotify.IsBackground = True

此行当应用程序

结束时,指示线程停止所有活动。当调试器终止或

网页关闭或会话结束时,应用程序不会结束。所以你的线程会继续运行。


如果iis重置,你的线程将因为应用程序

结束而终止,但它会立即启动提出了一个请求,这可能就是为什么你会在结束后看到电子邮件。当应用程序结束时,线程不可能运行




基本上,你需要重新考虑你的逻辑,因为线程将永远只要网页收到初始请求,就会运行
。这些

线程不会停止,直到iis重置或asp.net检测到更改为

配置文件。


-

问候,

Alvin Bruney

[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]

得到花絮?在此处获取... http://tinyurl.com/27cok

谁是dat? < WH ****** @ dot.com>在消息中写道

news:10 ************* @ corp.supernews.com ...

考虑以下代码启用ASP.Net
应用程序中的多线程我正在编写:

global.asx中的代码Application_start子例程,Threadnotify被声明为
全局作为新线程使用地址下面的EmailNotify:

ThreadNotify.IsBackground = True

ThreadNotify.Start()

--------

Public Sub EmailNotify()

昏暗的电子邮件作为新的ForumEmail

Do While ContinueNotify''布尔值设置为True所以循环将
继续

emails.EmailNotify()

Thread.Sleep(notifyinterval * 60000)''notifyinterval是通过web.config文件中的app
设置设置的



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

来自Application_End子程序中的global.asx文件

ThreadNotify.Abort()

如你所知一个可能的说法,当
应用程序启动时会发生什么是Threadnotify。子程序只是循环并调用一个
子程序,然后以几分钟的间隔暂停。好消息:这个有效。
坏消息:这个效果太好了。问题是,这个帖子什么时候终止?我可以将循环中的布尔值通知设置为false,这样
线程将到达它的代码的末尾,它将终止。但是,超越
这个
我有顾虑。我正在我的机器上编写一个应用程序并对其进行测试。
我不能让那个线程停止,除非我在该循环中将布尔值设置为false,这是不好的。当我结束应用程序的调试执行时,
线程继续并且它不会停止。哎呀,我可以停止IIS服务
并且线程仍然继续执行。我怎么知道它正在执行。该主题的目标是发送电子邮件。我收到的电子邮件表明
告诉我这个帖子还在运行。

这会导致更大的问题。当我为其他人发布此应用程序
使用时,即使用户可能会结束Web服务,我怎样才能确保该线程不会继续运行?这个傻瓜只是继续前进。

假设我将它上传到生产网络服务器。线程启动并且执行正常。假设我对应用程序代码进行了一些更改,并将必要的文件复制到生产Web服务器。当一个新的线程由更新的代码启动时,来自网站的前一个版本的线程是否会继续,即将有两个线程做同样的事情?

方式这个线程运行,我相信我可以停止web服务并销毁web应用程序。线程仍然会运行。这是不可接受的。
我如何确保这个线程停止?

这个线程运行在什么位置?我本以为线程会在IIS下运行
并认为IIS是父进程。但是,我终止了IIS,这个线程仍在继续。什么运行
下面?

最终,什么时候这个线程终止了?!我唯一可以做的就是停止这个线程就是重启机器!我很害怕将这个
发布到我的网站,因为它可能会启动并且永远不会停止,直到他们重新启动他们的机器。

这个帖子应该是在
用户确定暂停后发送电子邮件。
还有其他方法吗?我虽然使用了一个计时器,当
X
分钟数通过时,事件就会发生。我可以捕获那个
事件并执行电子邮件代码。我不知道该怎么做。
建议?

Chris Smith



" who be dat?" < WH ****** @ dot.com>在消息中写道

news:10 ************* @ corp.supernews.com ...

考虑以下代码启用ASP.Net
应用程序中的多线程我在写:


....

建议?



是的。我建议你立即停止使用多线程

与ASP.NET的所有想法!假装线程不存在,或者任何尝试使用ASP.NET的线程会给你造成可怕的问题(

true)。


现在,既然没有人听过这些简单的建议,我会尝试

来解释。在每个请求中,ASP.NET实例化您的Page对象,

在几个阶段处理它,然后销毁它。在请求完成后,

页面上的任何内容都不会保留。这使得您在请求期间启动的任何

线程变得非常困难,如果它们在请求终止之前没有终止,因为它们将在死页。


请求不会等待你的线程。它并不知道

它们存在。您的线程仍然存在且仍在引用您在页面上创建的
对象的事实将无法保存您 - 对象可能仍然存在,但是它们的状态可能无效。这尤其适用于ASP.NET创建或操作的任何对象,因为ASP.NET认为请求

已结束,因此不太可能继续保持任何对象的状态

属于它。


我强烈建议你学习一下ASP.NET的一些细节

在幕后做。看看下面的MSDN文章:


ASP.NET页面对象模型

http://msdn.microsoft.com/library/de ... - / dnaspp / htm

l / aspnet-pageobjectmodel.asp?frame = true)

开发ASP.NET服务器控件

http ://msdn.microsoft.com/library/de ... -us / cyguide / ht

ml / cpconkeyconceptsinwebformscontroldevelopment.asp)

-

约翰·桑德斯

johnwsaundersiii在hotmail


嗨克里斯,


为什么你的线程在这个无限循环中运行?难道不存在一些

逻辑来终止循环吗?除非你计划为你的线程创建某种池管理器,否则需要在你的线程中使用
自毁逻辑。如果你确实有这样的机制,你可以在某些全局(全局工作)或静态属性

上使用静态属性,这可以由应用程序关闭触发。


正如其他人在这里所说的那样,我会非常警惕从ASP.Net中解雇线程

,除非你确切知道你在做什么。如果一切都是自包含的

并且不依赖于页面的任何内容(这通常意味着你需要一个新的对象来复制你需要的b $ b),然后运行额外的线程是安全的。但是你

仍然需要某种退出策略。做你需要做的事 - 慢慢或

否则退出!


你可以做的另一件事是查看异步Web请求,这是

相对容易设置和运行。但是,对于

而言,实际上并没有什么好处,只是首先要绑定一个ASP.Net线程。


+++ Rick ---


-


Rick Strahl

West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/

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

开启浪潮网页

谁是dat? < WH ****** @ dot.com>在消息中写道

news:10 ************* @ corp.supernews.com ...

考虑以下代码启用ASP.Net
应用程序中的多线程我正在编写:

global.asx中的代码Application_start子例程,Threadnotify被声明为
全局作为新线程使用地址下面的EmailNotify:

ThreadNotify.IsBackground = True

ThreadNotify.Start()

--------

Public Sub EmailNotify()

昏暗的电子邮件作为新的ForumEmail

Do While ContinueNotify''布尔值设置为True所以循环将
继续
emails.EmailNotify()

Thread.Sleep(notifyinterval * 60000)''notifyinterval是通过web.config文件中的app
设置设置的

循环

End Sub

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

来自global.asx文件在Application_End子程序中

ThreadNotify.Abort()

尽可能可能会说,当
应用程序启动时会发生什么是Threadnotify。子程序只是循环并调用一个
子程序,然后以几分钟的间隔暂停。好消息:这个有效。
坏消息:这个效果太好了。问题是,这个帖子什么时候终止?我可以将循环中的布尔值notify设置为false,这样
线程将到达它的代码的末尾,它将终止。但是,超过
这个我有顾虑。我正在我的机器上编写一个应用程序并对其进行测试。
我不能让那个线程停止,除非我在该循环中将布尔值设置为false,这是不好的。当我结束应用程序的调试执行时,
线程继续并且它不会停止。哎呀,我可以停止IIS服务
,线程仍然继续执行。我怎么知道它正在执行。该主题的目标是发送电子邮件。我收到的电子邮件显示
告诉我这个帖子仍在运行。

这会导致更大的问题。当我为其他人发布这个应用程序
时,即使用户可能会结束Web服务,我怎样才能确保这个线程不会继续运行?这个傻瓜只是继续前进。

假设我将它上传到生产网络服务器。线程启动并且执行正常。假设我对应用程序代码进行了一些更改,并且
将必要的文件复制到生产Web服务器。当一个新的线程由更新的代码启动时,来自网站的前一个版本的线程是否会继续,即将有两个线程做同样的事情?

方式这个线程运行,我相信我可以停止web服务并销毁web应用程序。线程仍然会运行。这是不可接受的。
我如何确保这个线程停止?

这个线程运行在什么位置?我本以为线程会在IIS下运行
,并认为IIS是父进程。但是,我终止了IIS,这个线程仍在继续。什么运行
下面?

最终,什么时候这个线程终止了?!我
唯一可以阻止这个线程的是重启机器!我很害怕将这个
发布到我的网站,因为它可能会启动并且永远不会停止,直到他们重新启动他们的机器。

此线程应该在发送后发送电子邮件暂停由
用户确定。还有另一种方法吗?我虽然使用了一个计时器,当
X分钟数通过时,该事件将会触发。我可以捕获那个
事件并执行电子邮件代码。我不知道该怎么做。
建议?

Chris Smith



Consider the following code which enables multithreading in an ASP.Net
application I''m writing:

Code in global.asx Application_start subroutine, Threadnotify is declared
Globally as a new thread where it uses the address of EmailNotify below:

ThreadNotify.IsBackground = True

ThreadNotify.Start()

--------

Public Sub EmailNotify()

Dim emails As New ForumEmail

Do While ContinueNotify ''boolean value set to True so loop will continue

emails.EmailNotify()

Thread.Sleep(notifyinterval * 60000) ''notifyinterval is set via app
settings in web.config file

Loop

End Sub

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

From global.asx file in Application_End subroutine

ThreadNotify.Abort()

As you can probably tell, what happens is Threadnotify is started when the
application starts. The subroutine simply loops through and calls a
subroutine then it pauses in intervals of minutes. Good news: this works.
Bad news: this works too good. Problem is, when does this thread get
terminated? I can set the boolean notify in the loop to false so the thread
will reach the end of it''s code and it will terminate. However, beyond this
I have concerns. I''m writing an application on my machine and testing it.
I can''t get that thread to stop unless I set the boolean value to false in
that loop which is bad. When I end the debug execution of the app, the
thread continues and it doesn''t stop. Heck, I can stop the IIS Service and
the thread still continues to execute. How do I know it''s executing. The
goal of this thread is to send emails. I''m getting emails indicating which
tells me this thread is still running.

This leads to a larger issue. When I release this application for others to
use, how can I ensure this thread won''t keep running even though a user
might end the web service? This sucker just keeps going and going.

Suppose I upload this to a production webserver. The thread starts and
executes fine. Suppose I make some changes to the application code and copy
the necessary files to the production webserver. Will the thread from the
previous versoin of the website continue while a new thread is started by
the updated code i.e. there will be two threads doing the same thing?

The way this thread runs, I believe I can stop the webservice and destroy
the webapplication. The thread would still run. This isn''t acceptable.
How do I ensure this thread stops?

What is this thread running under? I would''ve thought the thread would run
under IIS and consider IIS to be the parent process. However, I''m
terminating IIS and this thread still keeps going. What is it running
under?

Ultimately, when the heck does this thread terminate?! The only thing I can
do to stop this thread is to reboot the machine! I''m scared to publish this
to my website as it may start up and never stop until they reboot their
machines.

This thread is supposed to send emails after a pause determined by the user.
Is there another way to do this? I though about using a timer that, when X
amount of minutes passed, then the event would fire. I could capture that
event and execute the email code. I wasn''t sure how to do this though.
Suggestions?

Thanks

Chris Smith

解决方案

couple issues.

ThreadNotify.IsBackground = True
this line instructs the thread to cease all activity when the application
ends. the application doesn''t end when the debugger terminates, or the
webpage is closed or session ends. so your thread will keep running.

in the event of an iis reset, your thread will end because the application
ends, but it starts as soon as a request is made which is probably why you
are seeing emails after it has ended. it''s not possible for the thread to be
running when the application has ended.

basically, you need to rethink your logic because threads will always be
running as long as the webpage has received an initial request. these
threads won''t stop until iis resets or asp.net detects a change to a
configuration file.

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
"who be dat?" <wh******@dot.com> wrote in message
news:10*************@corp.supernews.com...

Consider the following code which enables multithreading in an ASP.Net
application I''m writing:

Code in global.asx Application_start subroutine, Threadnotify is declared
Globally as a new thread where it uses the address of EmailNotify below:

ThreadNotify.IsBackground = True

ThreadNotify.Start()

--------

Public Sub EmailNotify()

Dim emails As New ForumEmail

Do While ContinueNotify ''boolean value set to True so loop will
continue

emails.EmailNotify()

Thread.Sleep(notifyinterval * 60000) ''notifyinterval is set via app
settings in web.config file

Loop

End Sub

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

From global.asx file in Application_End subroutine

ThreadNotify.Abort()

As you can probably tell, what happens is Threadnotify is started when the
application starts. The subroutine simply loops through and calls a
subroutine then it pauses in intervals of minutes. Good news: this works.
Bad news: this works too good. Problem is, when does this thread get
terminated? I can set the boolean notify in the loop to false so the
thread
will reach the end of it''s code and it will terminate. However, beyond
this
I have concerns. I''m writing an application on my machine and testing it.
I can''t get that thread to stop unless I set the boolean value to false in
that loop which is bad. When I end the debug execution of the app, the
thread continues and it doesn''t stop. Heck, I can stop the IIS Service
and
the thread still continues to execute. How do I know it''s executing. The
goal of this thread is to send emails. I''m getting emails indicating
which
tells me this thread is still running.

This leads to a larger issue. When I release this application for others
to
use, how can I ensure this thread won''t keep running even though a user
might end the web service? This sucker just keeps going and going.

Suppose I upload this to a production webserver. The thread starts and
executes fine. Suppose I make some changes to the application code and
copy
the necessary files to the production webserver. Will the thread from the
previous versoin of the website continue while a new thread is started by
the updated code i.e. there will be two threads doing the same thing?

The way this thread runs, I believe I can stop the webservice and destroy
the webapplication. The thread would still run. This isn''t acceptable.
How do I ensure this thread stops?

What is this thread running under? I would''ve thought the thread would
run
under IIS and consider IIS to be the parent process. However, I''m
terminating IIS and this thread still keeps going. What is it running
under?

Ultimately, when the heck does this thread terminate?! The only thing I
can
do to stop this thread is to reboot the machine! I''m scared to publish
this
to my website as it may start up and never stop until they reboot their
machines.

This thread is supposed to send emails after a pause determined by the
user.
Is there another way to do this? I though about using a timer that, when
X
amount of minutes passed, then the event would fire. I could capture that
event and execute the email code. I wasn''t sure how to do this though.
Suggestions?

Thanks

Chris Smith



"who be dat?" <wh******@dot.com> wrote in message
news:10*************@corp.supernews.com...

Consider the following code which enables multithreading in an ASP.Net
application I''m writing:
....
Suggestions?



Yes. I suggest that you immediately cease all idea of using multithreading
with ASP.NET! Pretend that threads just don''t exist, or that any attempt to
use threads with ASP.NET will cause horrible problems for you (which is
true).

Now, since nobody ever listens to such simple-minded suggestions, I''ll try
to explain. On every request, ASP.NET instantiates your Page object,
processes it in several phases, then destroys it. Nothing that was on the
page will remain after the request completes. This makes it very hard on any
threads you''ve started during the request, if they do not terminate before
the request terminates, as they will be operating on a dead page.

The request will not wait for your threads. It doesn''t have any idea that
they exist. The fact that your threads still exist and are still referencing
objects you created on the page will not save you - the objects may still
exist, but their state may invalid. This especially goes for any objects
created or manipulated by ASP.NET, since ASP.NET believes that the request
is over, and so is unlikely to continue to maintain the state of any objects
which belong to it.

I strongly recommend that you learn some of the details of what ASP.NET is
doing behind the scenes. Take a look at the following MSDN articles:

The ASP.NET Page Object Model
(http://msdn.microsoft.com/library/de...-us/dnaspp/htm
l/aspnet-pageobjectmodel.asp?frame=true)

Developing ASP.NET Server Controls
(http://msdn.microsoft.com/library/de...-us/cpguide/ht
ml/cpconkeyconceptsinwebformscontroldevelopment.asp)
--
John Saunders
johnwsaundersiii at hotmail


Hi Chris,

Why is your thread running in this endless loop? Shouldn''t there be some
logic to terminate the loop in and of itself? unless you''re planning on
creating some sort of pool manager for your thread(s) there needs to be
self-destruct logic in your threads. If you do have some such mechanism you
can use a static property on some global (Global works) or static property
that can get triggered by Application shutdowns.

As others have said here I''d be very wary of firing threads out of ASP.Net
unless you know exactly what you''re doign. If everything''s self contained
and doesn''t rely on anything of the page (which usually means copyin what
you need to a new object), then running additional threads is safe. But you
still need an exit strategy of some sort. Do what you need to do - slow or
otherwise and get out!

The other thing that you can do is look into async Web requests, which are
relatively easy to set up and run. But there''s really no huge benefit for
this over just tieing up an ASP.Net thread either in the first place.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web
"who be dat?" <wh******@dot.com> wrote in message
news:10*************@corp.supernews.com...

Consider the following code which enables multithreading in an ASP.Net
application I''m writing:

Code in global.asx Application_start subroutine, Threadnotify is declared
Globally as a new thread where it uses the address of EmailNotify below:

ThreadNotify.IsBackground = True

ThreadNotify.Start()

--------

Public Sub EmailNotify()

Dim emails As New ForumEmail

Do While ContinueNotify ''boolean value set to True so loop will continue
emails.EmailNotify()

Thread.Sleep(notifyinterval * 60000) ''notifyinterval is set via app
settings in web.config file

Loop

End Sub

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

From global.asx file in Application_End subroutine

ThreadNotify.Abort()

As you can probably tell, what happens is Threadnotify is started when the
application starts. The subroutine simply loops through and calls a
subroutine then it pauses in intervals of minutes. Good news: this works.
Bad news: this works too good. Problem is, when does this thread get
terminated? I can set the boolean notify in the loop to false so the thread will reach the end of it''s code and it will terminate. However, beyond this I have concerns. I''m writing an application on my machine and testing it.
I can''t get that thread to stop unless I set the boolean value to false in
that loop which is bad. When I end the debug execution of the app, the
thread continues and it doesn''t stop. Heck, I can stop the IIS Service and the thread still continues to execute. How do I know it''s executing. The
goal of this thread is to send emails. I''m getting emails indicating which tells me this thread is still running.

This leads to a larger issue. When I release this application for others to use, how can I ensure this thread won''t keep running even though a user
might end the web service? This sucker just keeps going and going.

Suppose I upload this to a production webserver. The thread starts and
executes fine. Suppose I make some changes to the application code and copy the necessary files to the production webserver. Will the thread from the
previous versoin of the website continue while a new thread is started by
the updated code i.e. there will be two threads doing the same thing?

The way this thread runs, I believe I can stop the webservice and destroy
the webapplication. The thread would still run. This isn''t acceptable.
How do I ensure this thread stops?

What is this thread running under? I would''ve thought the thread would run under IIS and consider IIS to be the parent process. However, I''m
terminating IIS and this thread still keeps going. What is it running
under?

Ultimately, when the heck does this thread terminate?! The only thing I can do to stop this thread is to reboot the machine! I''m scared to publish this to my website as it may start up and never stop until they reboot their
machines.

This thread is supposed to send emails after a pause determined by the user. Is there another way to do this? I though about using a timer that, when X amount of minutes passed, then the event would fire. I could capture that
event and execute the email code. I wasn''t sure how to do this though.
Suggestions?

Thanks

Chris Smith



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

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