Hello Greg,

The problem you are having is not unusual in this
type of situation. Usually every application you write runs in the
context of a Operating System process and therefore has disadvantages
of:

1) Poor scalability. Too many processes impair server performance.
(App domains are the preferred way of deploying an application
consisting of too many related assemblies or sub-applications. You can
have many isolated App Domains run within a single process thereby
improving performance and scalability.

2) Lack of isolation for the different tasks running within that same
application.
   Most times after application are closed or terminated, the host
process  continues in the background, consuming resources.

You can solve your problem by using App Domains.
Write the block of code in which the errors and exceptions exist has a
separate assembly or application and then load it as an App Domain in
the main application.

static void Main()
{
    // Create an Application Domain:
    System.AppDomain newDomain =
System.AppDomain.CreateDomain("MyDeleteAndReReadFileOp");

    // Load and execute an assembly:
    newDomain.ExecuteAssembly(@"c:\HelloWorld.exe");

    // Unload the application domain:
    System.AppDomain.Unload(newDomain);
}


Regards,

Programmer


---------- Forwarded message ----------
From: "Greg Hile" <[email protected]>
Date: Sep 30, 2:18 pm
Subject: Problems with Reading a file when using FileSystemWatcher
To: DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web
Services,.NET Remoting


I created a class that opens a file like so.

class FileReader

{

    public void ReadTextFile(string filePath)

    {

        using (TextReader reader = File.OpenText(filePath))

        {

           frm1.textBox1.Text = frm1.textBox1.Text + reader.ReadLine()
+
Environment.NewLine;

        }

        File.Delete(filePath);

    }

}

This works great when calling it from a button click event

private void button2_Click(object sender, EventArgs e)

{

    File.Copy(fileSource, fileDestination);

}

This also works if I use a timer to trigger the FileReader Class.

BUT, if I use a FileSystemWatcher Control to trigger the FileReader
class it
only works the first time.

After that it says the file is in use by another process.

Even though the old file is deleted and the new file is not open
anywhere.

I don't think it is a timing issue because I can wait several seconds
between and still get the same message.

Can anyone offer me any insights as to why this is happening?

I can live with using the timer control, but would rather use the
FileSystemWatcher

Greg

Reply via email to