views:

71

answers:

4

Hello,

First I'm going to explain what I'm trying to do, before asking the question directly. Basically, there is a Windows Service running on a specific machine, let's call it M1. This Windows Service runs a set of tasks using threads (1 thread for each partition of tasks). I need to program a functionality that will let the user stop/suspend/restart a partition from the administration panel of the application. My solutions consists in accessing those threads by their id/name. Once I have the thread I can do the right operation on it. Is it the right approach?

If yes, how do we do that? The threads are running in M1 and I need to access them from a remote computer (situated in the same network area). Is it even possible?

Thank you. Don't hesitate to ask for more explanations if needed.

+2  A: 

Giving direct control over thread management to a remote process is a hazardous way to solve this. You could set up a thread that listens for requests to suspend, etc., and manipulate the threads on behalf of the requester. But this can cause endless problems since you don't know what state the thread is in when you suspend it. In particular, it could have locks held, or be in the middle of a database transaction.

If tasks are short-lived, have each thread request work from a boss whenever it completes its current chore. To suspend a partition, tell the boss (via TCP, web services, or whatever) not to give the partition's thread any more work until further notice.

Marcelo Cantos
In fact the tasks are not short-lived. The reason why we want to stop/suspend partitions is that some tasks take a lot of time to finish and if we want to change a parameter for instance, we are obliged to wait until the task ends its execution.
Amokrane
@Amokrane: If you have to wait for it to finish before changing the parameters, how does it help to suspend the task?
Marcelo Cantos
In fact I was talking about the existant code. Providing this new functionnality will let people stop the thread and change the parameters before the end of the task.
Amokrane
@Amokrane: How are these parameters used by the task? How is it safe to change them while the thread is suspended, but not while it is running?
Marcelo Cantos
@Marcelo: Those parameters are just used for scheduling the task (StartTime/Period). If someone needs a task to be scheduled differently, he needs first to stop it because the scheduling is made only when the task restarts.The suspension can be useful, if a task is consuming too much CPU cycles (for instance, some tasks make import operations which can be very long and consuming). We suspend it, to let other tasks perform their operations more quickly.
Amokrane
By the way, sorry for the confusion. I just noticed that I said that the suspension will help changing the parameters, which is not true. Only stopping the task will. Recently, I've come up with a solution that doesn't involve managing the threads. Instead I'll work at the database level. What I've done works fine but..there is a risk that someone will stop/suspend someone else's task (if the name of the task and the name of the partition are exactly the same). I have to find a way to make the difference between those tasks. That's for the story :)
Amokrane
+2  A: 

I don't think that it's right approach. Will be better to have interface in service that will provide functionality to menage threads.

Orsol
You mean setting up a Webservice that will manage the threads on the remote machine?
Amokrane
Yes, WebService or Remoting
Orsol
Asp.Net Web Services and Remoting are legacy techonologies which have been replaced by WCF. And if by Web Services you meant WCF careful they are not the same thing.
Bear Monkey
I really meant Webservices.
Amokrane
+1  A: 

I would communicate to the process via TCP or .NET remoting with the parameters needed to define the task to perform.

Then you will need a class that manages this requests and communicates to the threads telling them what to do.

despart
What about a Webservice?
Amokrane
.Net remoting is rather dated. WCF-based services are easier to implement, configure and secure IMO.
Bear Monkey
Ok. Will do it through WCF.
Amokrane
+1  A: 

I would not use the thread id. The thread name is a possibility, but I would lean toward providing a name for each task and keying on that.

If I were tackling this problem, I would expose a Window Communication Foundation (WCF) interface on your Windows service. Your application would then control the tasks through that interface.

I would avoid using .NET Remoting. It will work, but .NET Remoting has been superseded by WCF. From the MSDN article:

WCF supersedes several earlier Microsoft technologies for creating distributed applications. Most applications that would have been built using ASP.NET Web Services, .NET Remoting, Enterprise Services, System.Messaging, or WSE will instead be built on WCF.

Matt Davis
Speaking of which (id/name), do yo think it's possible to get an instance of a thread from its name? By the way, I'm not going to use the id, it's not reliable.
Amokrane
AFAIK, there's no way to enumerate the list of managed threads. To do this, you might want to consider wrapping each thread in an object that you maintain. See here: http://stackoverflow.com/questions/1825882/getting-list-of-currently-active-managed-threads-in-c-net
Matt Davis