ASP.NET Desktop

A has an ASP.NET 2.0 web application that should allow you to send emails. I have a windows service that sends emails immediately. My web application composes an email message according to some template and puts it in MSMQ, and the service receives it from there.

The problem is that composing a message from a template may take some time, and I do not want the user to wait for the message to be compiled and submitted to the service.

I am thinking of some kind of background process that will listen on the internal queue of notification requests. If the queue is empty, the process does nothing, but as soon as the message appears, it starts processing the message. I want to have only one process, so as not to create many threads.

My current idea is to write a task scheduler that will contain a queue of notification requests. When a new item is added to the queue, the scheduler checks to see if the notification process is running. If so, just add the request to the queue. Otherwise, it creates a new thread that will read the queue until it becomes empty and executes notification requests.

My concern is that I must be sure that my thread will not die after ASP.NET completes the response to the client, because it is the parent thread for my thread. And the question is, what is the best way to do this (or can it be done)?

PS It's normal that my thread dies if IIS processes the ASP.NET process due to user inactivity.

+3
source share
3 answers

. . ASP.Net, , . ExecuteProcess while while (true) ", / " thread.sleep(500) " - . .

Imports System.Threading

Public MustInherit Class LongRunningProcess
    Public ReadOnly Property Running() As Boolean
        Get
            Return _Running
        End Get
    End Property

    Public ReadOnly Property Success() As Boolean
        Get
            Return _Success
        End Get
    End Property

    Public ReadOnly Property Exception() As Exception
        Get
            Return _Exception
        End Get
    End Property

    Public ReadOnly Property StartTime() As DateTime
        Get
            Return _StartTime
        End Get
    End Property

    Public ReadOnly Property EndTime() As DateTime
        Get
            Return _EndTime
        End Get
    End Property

    Public ReadOnly Property Args() As Object
        Get
            Return _Args
        End Get
    End Property


    Protected _Running As Boolean = False
    Protected _Success As Boolean = False
    Protected _Exception As Exception = Nothing
    Protected _StartTime As DateTime = DateTime.MinValue
    Protected _EndTime As DateTime = DateTime.MinValue
    Protected _Args() As Object = Nothing
    Protected WithEvents _Thread As Thread

    Private _locker As New Object()

    Public Sub Execute(ByVal Arguments As Object)
        SyncLock (_locker)
            'if the process is not running, then...'
            If Not _Running Then
                'Set running to true'
                _Running = True
                'Set start time to now'
                _StartTime = DateTime.Now
                'set arguments'
                _Args = Arguments
                'Prepare to process in a new thread'
                _Thread = New Thread(New ThreadStart(AddressOf ExecuteProcess))

                'Start the thread'
                _Thread.Start()
            End If
        End SyncLock
    End Sub

    Protected MustOverride Sub ExecuteProcess()
End Class
+1

Windows, ? , , , , - Windows, . , , .

, , , , , ( ). -, , . , .

+1

-, ASP.NET. . , .

0

Source: https://habr.com/ru/post/1794178/


All Articles