Google Anlatics

Sunday, August 7, 2011

.Net 4 Smart Thread Pool With Windows Service

Smart tread pool is third part tool and hear is link for more information about it.

This is best alternative for customize tread which is better than .net tread pool.

http://www.codeproject.com/KB/threads/smartthreadpool.aspx

Keep Concurrent Threads in App Config.

<appSettings>

<add key="ConcurrentThreads" value="5"/>

appSettings>

Import Following Namespace including Imports Amib. Threading which is for Smart Thread Pool.

Imports System.Diagnostics

Imports System.Threading

Imports Amib.Threading

Imports System.ServiceProcess

Imports Microsoft.Practices.Unity

This is Little Sample Code


Partial Public Class Service

Inherits ServiceBase

Private _threadPool As SmartThreadPool

Private _trace As New TraceSource("Import")


Sub New()

InitializeComponent()

End Sub



Private Shared _allowLoop As Boolean = True

Public Shared Sub Main(ByVal args As String())

Dim service As New TestService()

If args.Contains("console") OrElse Environment.UserInteractive Then

' disallow loops if passed a param

If args.Length > 1 Then

_allowLoop = False

End If

service.OnStart(args)

service.OnStop()

Else

ServiceBase.Run(service)

End If

End Sub


Protected Overrides Sub OnStart(ByVal args() As String)

_trace.TraceInformation("Starting Test...")


With Service.ServiceLocator.Container

.RegisterType(Of Service.IService,

End With

AddHandler Test.Test, AddressOf OnTest


_threadPool = New SmartThreadPool

_threadPool.MaxThreads=System.Configuration.ConfigurationManager.AppSettings("ConcurrentThreads")

_threadPool.MinThreads = 1

_threadPool.Start()



_trace.TraceInformation("ThreadPool started with maxthreads = {0}.", _threadPool.MaxThreads)



Dim worker As New Thread(AddressOf DoWork)

worker.Start()

End Sub

Private Sub DoWork()

_trace.TraceInformation("Worker started.")

 While _allowLoop ' by default, this is an infinite loop

Try

Console.WriteLine("Loop!")

'Check if we need to run some import

Dim ofrImpId = ImportQueue.Dequeue()

If Not ofrImpId.Equals(Guid.Empty) Then

_threadPool.QueueWorkItem(AddressOf Import, ofrImpId)

End If

'put Job to queue if scheduled

JobSchedule()

Catch ex As Exception

_trace.TraceEvent(TraceEventType.Error, 0, "EXPLOSION: {0}", ex.ToString)

 End Try

Thread.Sleep(TimeSpan.FromSeconds(15))

 End While

_threadPool.Shutdown()

 _threadPool.Dispose()

 _trace.TraceInformation("Worker finished.")

End Sub

Private Sub JobSchedule ()

Code Hear………………………………………………………………………..

_trace.TraceInformation("{0} enqueued!", lImport.ofrImpUrl)

End Sub

 Private Sub Working Process(ByVal ofrImpId As Guid)

Try

Working Code Hear()

Try

ImportManager.ImportOffers(job)

Catch ex As Exception

_trace.TraceEvent(TraceEventType.Error, 0, "failed with exception", )

Finally

End Try

Catch ex As Exception

_trace.TraceEvent(TraceEventType.Error, 0, " failed with exception: {0}.")

End Try

End Sub


No comments:

Sri Lanka .NET 
                Forum Member