Need Advice with threading/delegates in a service

Alt_F4

Member
Joined
Nov 6, 2008
Messages
6
Programming Experience
1-3
Hi All,

I am seeking some advice on whether i should use multi-threading or delegates in a service that i am writing.

I have written a couple of services before (with multi-threading) but think that i could have done a better job of it.

The service does not have a UI, it only needs to interact with a database and around 800 ip addresses.

I just need advice on which way would be better in terms of efficiency, if for example i needed to send data to all 800 ip addresses at once.

Please let me know if you would like more detailed information.

Thanks in advance
 
Well, you're certainly not going to create 800 threads. What you might like to consider is using the ThreadPool. You call its QueueUserWorkItem method 800 times and then it will decide when to execute each item and on which thread. The ThreadPool has a certain number of worker threads available to it and it will execute each item as a thread becomes available. You'd end up getting multiple messages sent simultaneously but not so many that you end up overloading the system with threads.
 
OK, thanks - yeah the services that i have written were only for a small amount of ips, so i wasn't sure how to handle such a large number of them. I will look into the ThreadPool and see how i go.

Cheers
 
Here's a quick example:
VB.NET:
Module Module1

    Sub Main()
        For i As Integer = 1 To 100
            Threading.ThreadPool.QueueUserWorkItem(AddressOf DoSomething, i)
        Next
    End Sub

    Private Sub DoSomething(ByVal data As Object)

    End Sub

End Module
Whatever method you use to do the work (in this case DoSomething) must match the WaitCallback delegate signature, so it must have one parameter of type object and no return value. Whatever you pass as the second argument when calling QueueUserWorkItem is the data you get back from the parameter of your method (in this case 'data'). Because the parameter is type Object you can pass anything you like. It's up to you to cast it and use it appropriately in your method.
 
Depending on the transport you may want to avoid threads/threadpool altogether and instead for example use available asynchronous socket methods, but that would be speculation as you haven't said anything about what you're doing with those IPs.
 
Depending on the transport you may want to avoid threads/threadpool altogether and instead for example use available asynchronous socket methods, but that would be speculation as you haven't said anything about what you're doing with those IPs.

I'd agree that allowing the system to manage its threadpool for socket writing would be better.. I read about something in .NET called XF, which can be used for higher preformance networking:
CodeProject: High Performance TCP/IP Server using C#.NET. Free source code and programming help
Developersdex.com - Multi-threaded TCP/IP Server without .NET Socket Class (mirror)
 
Back
Top