为什么这个线程的方法行不通? [英] Why does this threading approach not work?

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

问题描述

我有一个奇怪的问题,线程在ASP.NET应用程序。出于某种原因,当我在请求的线程运行code,一切都按预期工作。但是,当我在一个单独的线程中运行它,什么都不会发生。
这是通过调用下面的处理程序上的上三旗核实,关和larma分别 - 在一切正常的两个第一例,但在后者的什么也没有发生。

I have a wierd problem with threading in an ASP.NET application. For some reason, when I run the code in the request thread, everything works as expected. But when I run it in a separate thread, nothing happens.
This is verified by calling the below handler with the three flags "on", "off" and "larma" respectively - in the two first cases everything works, but in the latter nothing happens.

我在做什么错在这里?

在网络项目中,我有一个通用的处理程序如下code:

In the web project I have a generic handler with the following code:

If task = "on" Then
    Alarm.StartaLarm(personId)
    context.Response.Write("Larmet är PÅ")
ElseIf task = "off" Then
    Alarm.StoppaLarm(personId)
    context.Response.Write("Larmet är AV")
ElseIf task = "larma" Then
    Alarm.Larma(personId)
    context.Response.Write("Larmar... (stängs av automagiskt)")
Else
    context.Response.Write("inget hände - task: " & task)
End If

警报类有以下方法:

Public Shared Sub Larma(ByVal personId As Integer)
    Dim thread As New System.Threading.Thread(New ParameterizedThreadStart(AddressOf Larma_Thread))
    thread.Start(personId)
End Sub

Private Shared Sub Larma_Thread(ByVal personId As Integer)
    StartaLarm(personId)
    Thread.Sleep(1000 * 30)
    StoppaLarm(personId)
End Sub

Public Shared Sub StartaLarm(ByVal personId As Integer)
    SandSMS(True, personId)
End Sub

Public Shared Sub StoppaLarm(ByVal personId As Integer)
    SandSMS(False, personId)
End Sub

Public Shared Sub SandSMS(ByVal setOn As Boolean, ByVal personId As Integer)
    ...
End Sub

更新/澄清:我仍然可以给客户预期的响应 - 没有错误信息 - 调用线程版本时

UPDATE/CLARIFICATION: I still get the expected response to the client - no error messages - when calling the threaded version.

我也包含在code以上一个被遗忘的方法。

I also included a forgotten method in the code above.

更新2: @Henk,不幸的是我没有能力来调试,因为这个问题只出现在我们的尖锐服务器,它没有安装Visual Studio和不允许远程调试。

Update 2: @Henk, Unfortunately I don't have the ability to debug, because this problem arises only on our sharp server, which doesn't have Visual Studio installed and doesn't allow remote debugging.

不过, SendSMS 方法来发送短信到我手机上,并且短信Web服务,我的电话同意该消息被开时调用发送或叫larma时,关闭,但不是。

However, the SendSMS method sends text messages to my phone, and both the text message web service and my phone agrees that the messages are sent when calling "on" or "off", but not when calling "larma".

因为我知道,整个产业链处理程序 - > StartaLarm - > SandSMS(真/假)工程开和关,我必须承担所发生的故障某处处理程序 - > Larma - > Larma_Thread ,因而是一个线程问题

Since I know that the entire chain Handler->StartaLarm->SandSMS(True/False) works for "on" and "off", I must assume that the failure occurs somewhere in Handler->Larma->Larma_Thread, and thus is a threading issue.

更新3 :@Vadmyst,将您的code到VB.NET(这是不是我最喜欢的两任,但这个项目需要它...)和修改后编译,我得出了以下(虽然我不是100%肯定它仍然意味着同样的事情...):

Update 3: @Vadmyst, after converting your code to VB.NET (which is not my favourite of the two either, but this project requires it...) and modifying it to compile, I arrived at the following (although I'm not 100% certain it still means the same thing...):

ThreadPool.QueueUserWorkItem(New WaitCallback(Function(p As Integer) Larma_Thread(p)))

没有成功 - 我有与上述相同的结果... =(

No success - I have the same results as above... =(

推荐答案

您在SandSMS从StartaLarm称为从Larma_Thread在辅助线程的上下文中调用一个例外。在什么也没发生在一个多线程应用程序的行为是例外的一个辅助线程一个强烈的信号。我想到的第一件事,就是在Web应用程序中的非请求的线程不同,是一个非请求的线程没有获得的东西,如当前HttpContext和会话。

You have an exception in SandSMS called from StartaLarm called from Larma_Thread in the context of a secondary thread. The "nothing happens" behaviour in a multi-threaded application is a strong indication of exception in a secondary thread. The first thing that comes to mind, that is different in a non-request thread in a web application, is that a non-request thread does not have access to things such as current HttpContext and Session.

这篇关于为什么这个线程的方法行不通?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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