线程问题 [英] Threading question

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

问题描述

我的C#代码中有一些相当复杂的业务逻辑,在ASP.NET页面调用的类
中。执行大约需要3-4秒。它不依赖于SQL调用或类似的东西。


我知道工作进程一次做两件事的问题

主线程池(旧的坐在一个紧密的循环中20秒,并观察

所有的webapps死!问题)。因此,我已经解决了这个问题,将业务逻辑产生到新线程中。主线程Sleep()直到它完成了b $ b,然后继续。例如:


Button_Click:

1)开始新线程

2)睡觉直到完成

3)销毁它


新主题:

1)做几秒钟的重物

2)终止


我想,由于主线程正在等待我的新线程,

我不需要锁定()任何对象,我可以直接访问。


这一切似乎工作正常,并解决了我的其他用户正在体验的其他用户的奇怪延迟。 />

这听起来不错吗?


谢谢,


John

I''ve got some reasonably complex business logic in my C# code, in a class
called by a ASP.NET page. This takes around 3-4 seconds to execute. It''s not
dependent on SQL calls or anything like that.

I know of the problems with the worker process doing two things at once from
the main thread pool (The old "Sit in a tight loop for 20 seconds and watch
all your webapps die!" issue). So I''ve worked around the issue by spawning
the business logic into a new thread. The main thread Sleep()''s until it''s
finished, and then continues. eg:

Button_Click:
1) start new thread
2) Sleep until it''s done
3) destroy it

New thread:
1) Do something heavy for a few seconds
2) terminate

I figured that, since the main thread is sleeping waiting for my new thread,
I don''t need to lock() any objects, I can just access then directly.

It all seems to work fine, and has solved the strange delays my other
webapp-users were experiencing.

Does this sound like good practice?

Thanks,

John

推荐答案

嗨约翰:


我不熟悉紧紧围着20秒坐着看

webapps die"问题。对于WinForms应用程序来说这是一个问题

需要一个主线程来抽取消息来保持GUI响应,

但是在网络中没有''主线程''应用程序。


我认为你使应用程序比使用两个线程处理单个请求而不是一个线程所需的更复杂。

我不知道为什么你的用户会在没有

额外线程的情况下看到奇怪的延迟。额外的线程是否会执行任何工作来生成发送给客户端的结果?
结果?你怎么知道你正在睡觉

足够让工人线程完成?您使用的是EndInvoke

还是重置事件?


-

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

星期二,2004年9月28日18:59:32 +0100,John

< js ************ @ ecclesdeletethiscollege.ac.uk>写道:
Hi John:

I''m not familiar with "sit in a tight loop for 20 seconds and watch
the webapps die" issue. That is an issue for WinForms applications
which need a main thread to pump messages to keep the GUI responsive,
but there in no ''main thread'' in a web app.

I think you are making the app more complicated than it needs to be by
using two threads to process a single request instead of one thread. I
don''t understand why your users would see strange delays without the
extra thread. Does the extra thread perform any work to produce
results that are sent to the client? How do you know you are sleeping
long enough for the worker thread to finish? Are you using EndInvoke
or a reset event?

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 28 Sep 2004 18:59:32 +0100, "John"
<js************@ecclesdeletethiscollege.ac.uk> wrote:
我的C#代码中有一些相当复杂的业务逻辑,在ASP.NET页面调用的类中。执行大约需要3-4秒。它不依赖于SQL调用或类似的东西。

我知道工作进程在主线程池中同时做两件事的问题(旧的坐在一个紧密的循环中20秒,看着你所有的webapps死了!问题)。所以我通过将业务逻辑产生到一个新线程来解决这个问题。主线程Sleep()直到它完成,然后继续。例如:

按钮_点击:
1)开始新线程
2)睡觉直到它完成
3)销毁它

新线程:
1)做一些沉重的事情几秒钟
2)终止

我想,由于主线程正在等待我的新线程,
我不需要锁定()任何对象,我可以直接访问。

这一切似乎工作正常,并解决了我的其他的奇怪延迟
webapp-用户体验过。

这听起来像是一种好习惯吗?

谢谢,

约翰
I''ve got some reasonably complex business logic in my C# code, in a class
called by a ASP.NET page. This takes around 3-4 seconds to execute. It''s not
dependent on SQL calls or anything like that.

I know of the problems with the worker process doing two things at once from
the main thread pool (The old "Sit in a tight loop for 20 seconds and watch
all your webapps die!" issue). So I''ve worked around the issue by spawning
the business logic into a new thread. The main thread Sleep()''s until it''s
finished, and then continues. eg:

Button_Click:
1) start new thread
2) Sleep until it''s done
3) destroy it

New thread:
1) Do something heavy for a few seconds
2) terminate

I figured that, since the main thread is sleeping waiting for my new thread,
I don''t need to lock() any objects, I can just access then directly.

It all seems to work fine, and has solved the strange delays my other
webapp-users were experiencing.

Does this sound like good practice?

Thanks,

John






你设计的是没有任何线程优势的线程。

你创建一个线程,然后睡眠主线程直到它完成。

这完全等同于根本不创建新线程。线程的目的

允许同时执行2个不同的任务。


-

HTH,

Kevin Spencer

..Net开发人员

微软MVP

我得到了很高的报酬

解决难题为生活


" John" < JS ************ @ ecclesdeletethiscollege.ac.uk>在消息中写道

新闻:O#************** @ TK2MSFTNGP10.phx.gbl ...
What you''ve designed is threading without any of the benefits of threading.
You create a thread, and then sleep the main thread until it is finished.
This is exactly equivalent to not creating a new thread at all. The purpose
of threading is to allow 2 different tasks to be executed at the same time.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"John" <js************@ecclesdeletethiscollege.ac.uk> wrote in message
news:O#**************@TK2MSFTNGP10.phx.gbl...
我已经在我的C#代码中,在ASP.NET页面调用的类中获得了一些相当复杂的业务逻辑。执行大约需要3-4秒。这是
不依赖于SQL调用或类似的东西。

我知道工作进程在主线程池中一次做两件事
的问题(旧的坐在一个紧凑的循环中20秒,并且
观看你所有的webapps死!问题)。所以我通过将业务逻辑产生到一个新线程来解决这个问题。主线程Sleep()直到它完成,然后继续。例如:

按钮_点击:
1)开始新线程
2)睡觉直到它完成
3)销毁它

新线程:
1)做一些重的东西几秒钟
2)终止

我想,因为主线程正在等待我的新
线程,我不需要锁定()任何对象,我可以直接访问。

这一切似乎工作正常,并解决了我的其他的奇怪延迟
webapp-用户体验过了。

这听起来像是不错的做法吗?

谢谢,

约翰
I''ve got some reasonably complex business logic in my C# code, in a class
called by a ASP.NET page. This takes around 3-4 seconds to execute. It''s not dependent on SQL calls or anything like that.

I know of the problems with the worker process doing two things at once from the main thread pool (The old "Sit in a tight loop for 20 seconds and watch all your webapps die!" issue). So I''ve worked around the issue by spawning
the business logic into a new thread. The main thread Sleep()''s until it''s
finished, and then continues. eg:

Button_Click:
1) start new thread
2) Sleep until it''s done
3) destroy it

New thread:
1) Do something heavy for a few seconds
2) terminate

I figured that, since the main thread is sleeping waiting for my new thread, I don''t need to lock() any objects, I can just access then directly.

It all seems to work fine, and has solved the strange delays my other
webapp-users were experiencing.

Does this sound like good practice?

Thanks,

John



嗨斯科特,

我正在使用Windows 2000和XP,不知道它是否已在2003年修复。 .NET 1.1

sp1。


在webform上添加一个按钮,并在服务器事件处理程序上添加一些内容

如:


DateTime d = DateTime.Now.AddSeconds(20);

while(DateTime.Now< d);

运行表单,cl舔按钮。看到您的CPU跳转到100%,并尝试在另一个浏览器窗口中使用

另一个webapp。没有。死。填充:)至少

直到20秒过去。


但是如果你运行我建议的代码,其他应用程序仍保持响应这个

等待20秒......奇怪!即使CPU是100%,因为(我假设)

当相同优先级的另一个线程要求时间时,它会获得50%的

插槽繁忙 - 等待线程越来越多了。


我知道它在过去已被详细讨论过了。我知道坐在一个

紧的循环中20秒是一个愚蠢的事情;)但在我的现实应用中

我是号码 - 用数据集进行处理。可能2-4秒。


我检查新线程是否使用Thread.Join()完成,超时

数字。我想你也可以睡觉(200)或其他东西并进行调查

ThreadStatus。


我的担忧有两方面:

1)同步锁定可能有一些奇怪的问题,即使*我不是* b $ b同时在两个线程中使用它们,也许系统会以某种方式进行? />

2)我猜系统有一个线程池是有充分理由的,而我只是按需要创建线程的
。也许我可以超载系统?


谢谢,


John


" Scott艾伦QUOT; <位掩码@ [NOSPAM] .fred.net>在消息中写道

news:hu ******************************** @ 4ax.com ...
Hi Scott,

I''m using Windows 2000 and XP, don''t know if it''s fixed on 2003. .NET 1.1
sp1.

Add a button to your webform, and on the server event handler put something
like:

DateTime d = DateTime.Now.AddSeconds(20);
while (DateTime.Now < d) ;
Run the form, click the button. See your CPU jump to 100%, and try to use
another webapp in another browser window. Nothing. Dead. Stuffed :) At least
until 20 seconds have passed.

But if you run the code I suggested, other apps remain responsive for this
20 second wait.... strange! even though CPU is at 100%, because (I assume)
when the other thread of same priority asks for time, it gets 50% of the
slots the busy-wait thread was getting.

I know it''s been discussed at length in the past. I''m aware sitting in a
tight loop for 20 seconds is a stupid thing to do ;) but in my real-life app
I''m number-crunching with datasets. For maybe 2-4 seconds.

I check the new thread is finished using Thread.Join() with a timeout
figure. I guess you could also just Sleep(200) or something and poll
ThreadStatus.

My concerns are two-fold:

1) there might be some weird issue with sync locking, even though *I''m* not
using them in two threads simultaniously, maybe the system does somehow??

2) I guess the system has a thread-pool for a good reason, whereas I''m just
creating threads on demand. Maybe I could overload the system??

Thanks,

John

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:hu********************************@4ax.com...
嗨约翰:

我不熟悉坐在一个紧密的循环中20秒,看着webapps死了问题。对于WinForms应用程序来说,这是一个问题,需要一个主线程来抽取消息来保持GUI响应,但是在Web应用程序中没有主线程。

我认为你使应用程序比使用两个线程来处理单个请求而不是一个线程所需的更复杂。我不明白为什么你的用户会在没有
额外线程的情况下看到奇怪的延迟。额外的线程是否执行任何工作以产生发送给客户端的结果?你怎么知道你正在睡觉足够长的工作线程完成?您使用的是EndInvoke
还是重置事件?

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

2004年9月28日星期二18:59:32 +0100,John
< js ************ @ ecclesdeletethiscollege.ac.uk>写道:
Hi John:

I''m not familiar with "sit in a tight loop for 20 seconds and watch
the webapps die" issue. That is an issue for WinForms applications
which need a main thread to pump messages to keep the GUI responsive,
but there in no ''main thread'' in a web app.

I think you are making the app more complicated than it needs to be by
using two threads to process a single request instead of one thread. I
don''t understand why your users would see strange delays without the
extra thread. Does the extra thread perform any work to produce
results that are sent to the client? How do you know you are sleeping
long enough for the worker thread to finish? Are you using EndInvoke
or a reset event?

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 28 Sep 2004 18:59:32 +0100, "John"
<js************@ecclesdeletethiscollege.ac.uk> wrote:
我的C#代码中有一些相当复杂的业务逻辑,在ASP.NET页面调用的类中。执行大约需要3-4秒。它不依赖于SQL调用或类似的东西。

我知道工作进程一次做两件事的问题
来自<主线程池(旧的坐在一个紧密的循环中20秒,并且
观看所有的webapps死!问题)。所以我通过将业务逻辑产生到一个新线程来解决这个问题。主线程Sleep()直到它完成,然后继续。例如:

按钮_点击:
1)开始新线程
2)睡觉直到它完成
3)销毁它

新线程:
1)做一些沉重的事情几秒钟
2)终止

我想,因为主线程正在等待我的新线程,
我不需要锁定()任何对象,我可以直接访问。

这一切似乎工作正常,并解决了我的其他人的奇怪延迟/> webapp-用户正在体验。

这听起来像是一种好习惯吗?

谢谢,

John
I''ve got some reasonably complex business logic in my C# code, in a class
called by a ASP.NET page. This takes around 3-4 seconds to execute. It''s
not
dependent on SQL calls or anything like that.

I know of the problems with the worker process doing two things at once
from
the main thread pool (The old "Sit in a tight loop for 20 seconds and
watch
all your webapps die!" issue). So I''ve worked around the issue by spawning
the business logic into a new thread. The main thread Sleep()''s until it''s
finished, and then continues. eg:

Button_Click:
1) start new thread
2) Sleep until it''s done
3) destroy it

New thread:
1) Do something heavy for a few seconds
2) terminate

I figured that, since the main thread is sleeping waiting for my new
thread,
I don''t need to lock() any objects, I can just access then directly.

It all seems to work fine, and has solved the strange delays my other
webapp-users were experiencing.

Does this sound like good practice?

Thanks,

John



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

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