开发者

How to continue execution of code only when windows service is completed?

I have a Windows service which performs a fairly long running task. At the moment, I spawn a new thread, which executes a method which goes off and calls this windows service. This code looks like:

Thread thread = new Thread(new 开发者_开发知识库ThreadStart(ExecyuteLongRunningMethod));
thread.Start();
thread.Join();

Higher up in the callstack (when this code is done), a message box popups stating the result of the operation.

However, while this block of code executes (ExecuteLongRunningMethod calls the windows svc), a message box popups stating that nothing has changed as a result of the operation, but because this is before the code block above has completed, the wrong message box appears.

Thus, the question is, what would be the proper way to only continue execution in the winforms app (this is what calls the windows svc), ONLY when the windows service is finished? I am thinking that the approach above is incorrect as the thread will call the windows service (another process), so while the windows service does its stuff, my code (winforms app) will continue. Either some sort of signaling is required, or something like named pipes?

The app is in .NET 3.5.

Thanks


Expose the status of your service execution via a WCF API and call it using netTcp or named pipes, you could poll the service or use a WCF CallBack.


Why don't you use a BackgroundWorker, so you can set an event for its completion?
In Backgroundworker running method you can run the service and wait for a manual reset event (for example) or a mutex and, when received this you can exit main method.
So OnCompleted event is raised in your main thread (UI for example) and the job is done...

In your app add a Backgroundworker and name it bgw.
Then you can do:

private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
    // 1. Run the service
    // 2. Wait for mutex
}

private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // Here you can handle the end of the service
    // and return the status in main thread
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜