开发者

Controlling commandline process with Filewatcher in a windows service

I am trying to create snapshots using mplayer and a filewatcher in a windows se开发者_开发知识库rvice. I have created the service and I was able to get the commandline mplayer to create to snapshots, but I came into a problem. I need to create a queue of some sorts so that I don't have umpteen (scientific nomenclature) mplayer processes running at once and flooding the IO. I have tried creating a SynchronizedCollection to pull from, but I am having a problem understanding where I should start a controlling process that checks to see if a file is in queue and spawn a new thread to create the snapshots.


I had to implement something similar a while ago, here is what I did, here is the basic code sample. you can find the full class Here.

Here are the basics of what you need to do,

you need a queue, you can use any list data structure to store your queue, you need your file watcher to add new items to this queue.

you need another method that process the queue, something like this. (this will process item in the queue until the queue is empty)

    private void ProcessQueue()
    {
        do
        {
                try
                {
                    Itme job = null;

                    lock (Queue)
                    {
                        if (Queue.Count != 0)
                        {
                            job = Queue.First();
                            Queue.Remove(job);
                        }
                    }

                    if (job != null)
                    {
                        Execute(job);
                    }

                }
                catch (Exception e)
                {
                    Logger.FatalException("An error has occurred while processing queued job.", e);
                }


        } while (Queue.Count != 0);

        Logger.Trace("Finished processing jobs in the queue.");

        return;
    }

You also need to make sure that your file watcher will triggers a ProcessQeueue() if it's not already running, it should be easy enough to figure that out, you can either use a flag variable that you set or if you have a process instance you can check the status on your process instance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜