Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this threading approach not work?
    text
    copied!<p>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.<br> 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.</p> <p><strong>What am I doing wrong here?</strong></p> <p>In the web project I have a generic handler with the following code:</p> <pre><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: " &amp; task) End If </code></pre> <p>The <code>Alarm</code> class has the following methods:</p> <pre><code>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 </code></pre> <p><strong>UPDATE/CLARIFICATION:</strong> I still get the expected response to the client - no error messages - when calling the threaded version.</p> <p>I also included a forgotten method in the code above.</p> <p><strong>Update 2:</strong> @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. </p> <p>However, the <code>SendSMS</code> 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". </p> <p>Since I know that the entire chain Handler-><code>StartaLarm</code>-><code>SandSMS(True/False)</code> works for "on" and "off", I must assume that the failure occurs somewhere in Handler-><code>Larma</code>-><code>Larma_Thread</code>, and thus is a threading issue.</p> <p><strong>Update 3</strong>: @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...):</p> <pre><code>ThreadPool.QueueUserWorkItem(New WaitCallback(Function(p As Integer) Larma_Thread(p))) </code></pre> <p>No success - I have the same results as above... =(</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload