views:

276

answers:

4

I'm creating a win service that monitors ftp logs, when a file has been uploaded I want to start an external application, like a powershell script, to do stuff with the file. my Question is do i want to spin this off into another thread when i do it or should I just wait until it finishes before moving on.

This process is already going to be in a thread (the service is going to be monitoring multiple servers) and the idea of threads starting threads worries me. Is this something to be worried about or is this a case of too much tinfoil in my hat.

+2  A: 

Well, code it in a modular fashion and don't worry about threads. If, down the road, you find that your application will benefit from a multi-threaded approach then address it then. If you have build your components orthogonally then the threading part will fit more naturally.

Addressing threading concerns at the very beginning of an application always feel like premature optimization to me. Build the components first and worry about how to thread them later.

[Edit] I am in no way advising you to not think about threading at all. Every component needs to be build with the potential for use by multiple threads - this is a defensive and intelligent practice in all applications. What I meant was don't worry so much about how the application will handle threads and how to set up the thread management of the application first.

Andrew Hare
My dear, this is a dangerous advise. I also agree on not using threads if they don't have any observable advantage, but "worry about threading later" usually leads to not-thought-through half-way solutions full of threading-bugs, especially when you're not experienced in that kind of stuff.
gimpf
But surely the same can also be said that if you over think the design (with regard to threading or anything else) you could end up with an over-engineered application when it could have been a lot simpler in the first place.
RSlaughter
@gimpf,I completely agree. Designing with threads in mind for tasks such as IO (which this task really is) is very appropriate to do before hand.
Jason Jackson
I'm building in a lot of modularity so adding threading later isn't an issue, mostly it was worrying about threads creating threads. Someone said to just pass it to the thread pool I'm already using, but I lose modularity there and I want to keep this very loosely coupled
Bob The Janitor
+1  A: 

I think the more important question is what do you get out of spawning another thread? If you don't need to have the code execute in parallel, then don't do it. If you do, there should be no problem. If you are concerned with the child thread creating its own thread, then delegate the thread creation to the ThreadPool.

Michael Meadows
+1  A: 

The primary question: do you need to know the outcome of that process? If you can fire and forget, then do that - it's easier. If you need the outcome, then wait for it.

Also, have you considered using the FileSystemWatcher? It works remotely.

Max Schmeling
+1  A: 

Although somewhat off-topic, since you mentioned that you'll be launching a powershell script, I wanted to point out the option to run the script in-process via a powershell "runspace". Here's a minimal example:

using System.Management.Automation;
static class PoshExec
{
    static void Exec(string scriptFilePath)
    {
     (new RunspaceInvoke()).Invoke("& " + scriptFilePath);
    }
}

add a reference to c:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll

Scott Weinstein
Sweet Idea, but I don't want to limit myself to powershell. I want to make this as flexible as passable, at some point I may want to run winzip, sftp, or something else.
Bob The Janitor
of course, but it's it a console app you wish to run, you can always call them in the context of a powershell script.
Scott Weinstein