Zahymaka
Zahymaka

Reputation: 6621

Execution stops without any exception

I have some code to delete user profiles over the network using a service that runs on the client computers that looks like this:

public static void DeleteProfiles()
{
    ConsoleWrapper.WriteLine("We're in DeleteProfiles()");

    try
    {
        string[] users = GetUsers();
        ConsoleWrapper.WriteLine("Users gotten");

        if (users == null || users.Length == 0)
        {
            DeletingProfiles = false;
            ConsoleWrapper.WriteLine("Null or no user loaded");
            return;
        }

        DirectoryInfo oInfo = new DirectoryInfo("C:/Users");
        if (!oInfo.Exists)
            oInfo = new DirectoryInfo("C:/Documents and Settings");
        ConsoleWrapper.WriteLine("Profile folder gotten.");

        ConsoleWrapper.WriteLine("User Directory: " + oInfo.FullName);
        ConsoleWrapper.WriteLine("\nDirectories to be deleted:");

        DirectoryInfo[] SubDirectories = oInfo.GetDirectories();

        foreach (DirectoryInfo oSubDir in SubDirectories)
        {
            bool match = false;
            foreach (string user in users)
            {
                if (user == null)
                    continue;
                if (oSubDir.Name.ToLower() == user.ToLower())
                {
                    ConsoleWrapper.WriteLine("Matched: " + oSubDir.FullName);
                    match = true;
                    break;
                }
            }

            if (match)
                continue;
            try
            {
                ConsoleWrapper.WriteLine(oSubDir.FullName);
                DeletePath(oSubDir.FullName);
            }
            catch (Exception ex)
            {
                ConsoleWrapper.WriteLine(ex.StackTrace);
            }
        }
    }
    catch (Exception dirEx)
    {
        ConsoleWrapper.WriteLine(dirEx.Message);
    }
}

The WriteLine method from the ConsoleWrapper actually appends data to a txt file, because that's the easiest way I can think of to debug on a different machine.

This exact code works when I use it in a command-line application, but when I try to use it in the Windows service, it simply writes "We're in DeleteProfiles()." As you'll notice, I've caught every "catchable" exception, but I still don't understand what's going on.

All the client computers are exhibiting this problem, but it works well on both my desktop and laptop. What am I doing wrong?

For the DeletePath code, see http://www.vbdotnetforums.com/windows-services/16909-deletedirectory-throws-exception.html#post51470. I practically lifted it and converted it to C# code.

Upvotes: 1

Views: 1679

Answers (6)

Some Canuck
Some Canuck

Reputation: 846

Services require additional securities to interact with the desktop. There is an installer to provide this for you here: http://www.codeproject.com/KB/install/cswindowsservicedesktop.aspx

Upvotes: 2

CodeMonkeyKing
CodeMonkeyKing

Reputation: 4632

From a logging perspective I'd use a well known library like log4net or Microsoft's Logging Application Block. When you roll your own logging make sure that you call Flush() to clear any bufferred writes. I've found that log4net is easier to incorporate into existing code unless you're already an Enterprise Library shop.

Upvotes: 0

cdonner
cdonner

Reputation: 37668

If you can't figure it out, put the remote debugging monitor on a client and attach to it. You may have to put a Thread.Sleep() on top of your method so that you have enough time to find the service process in the client's process list and you are attached by the time it actually runs.

Upvotes: 1

Rasmus Faber
Rasmus Faber

Reputation: 49629

What does GetUsers() do?

Can it potentially take a long time?

How are you calling DeleteProfiles() from the service?

If you are calling DeleteProfiles() directly from an override of ServiceBase.OnStart(), you need to know that OnStart() times out after 30 seconds.

Upvotes: 2

Michael Meadows
Michael Meadows

Reputation: 28416

It's possible that you're not flushing ConsoleWrapper. Try putting a flush in a finally block, or turning on autoflush.

Upvotes: 2

JaredPar
JaredPar

Reputation: 754725

If you caught every catchable exception and you're not seeing an entry in the log then it's very likely that an un-catchable exception is being thrown. This is typically in the form of a StackOverflowException. By default a process cannot catch such an exception.

If DeletePath is a recursive function, try making it iterative and see if that fixes the problem.

Upvotes: 2

Related Questions