Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I got a service running in the background. It checks for xml files in a directory. Whenever a new file is present, the service validates the xml, and if the xml is valid, insert the data into a database.

 

After a while of testing, I figured weird results randomly occur if i stop the service while it's working. This is actually what i expected, so I began a search on the web on how to prevent a service from stopping, before a loop is completed.

 

Maybe it's my english, my way of using search engines or just me, but I could not find anything. Nothing about waiting till a loop is done before terminating the service. I though this was a quite "popular" subject...

 

Any suggestions?

 

Thanks,

-- Fredrik Aasgaard

Erutangis a si tahw
Posted

OK, this is not exactly what i was looking for, but it works...

 

In the loop of the service, around the critical part, lock the active thread like this:

lock(this)
{
    //critical part of loop
}

 

Now, when the service attempts to stop, it actually enters the OnStop function, completes all thasks there, but the locked thread will continue to execute till it leaves the lock block. After there this thread also terminates.

 

Hope this helps someone..

-- Fredrik Aasgaard

Erutangis a si tahw

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...