I have a somewhat complex question about how Threading
works across the network. Below is the full source code
of a program that will list any file modified on
myServer. When I run this code from myWorkstation and
search myServer using a network share (using a Thread set
to priority.Lowest) does myWorkstation inform myServer
that this is a low priority thread? Since myServer is a
production server, I don't want my file monitoring
program to preempt other services running on the server
in any way.
Obviously, when the code is executed on myWorkstation,
the local CPU is well aware that the Thread is Low
priority. But when the file search is running on the
remote machine (myServer) CPU cycles are being used on
both machines. If anyone has any documentation or code
that will explain that myServer (would)/(would not) be
informed that this is a low priority thread I would be
very interested.
Thanks.
/************************************************** *******
FileMonitor.cs
This class lists the files modified on the C:\ drive of a
server (\\\\myServer\\c$\\) that have
been modified in the last 24 hrs.
A lot of this code has been modified from (FileCopy.cs)
code written by Bill Gearhart found @
http://www.aspemporium.com/aspEmpori...p/howtos/howto.
asp?hid=20
Thanks Bill!
Note: Lookout for line wrapping. This should compile fine.
*************************************/
using System;
using System.IO;
using System.Threading;
class FileMonitor
{
static void Main()
{
Thread
thread = new Thread(new ThreadStart(MyMethod));
thread.Priority = ThreadPriority.Lowest;
thread.Start();
Console.WriteLine(thread.Priority);
}
private static void MyMethod()
{
string
srcdir = Path.Combine(Environment.GetFolderPath
(Environment.SpecialFolder.DesktopDirectory), "\\\\myServe
r\\c$\\");
DirectoryInfo dir;
FileInfo[] files;
DirectoryInfo[] dirs;
string tmppath;
dir = new DirectoryInfo(srcdir);
if (! dir.Exists)
{
throw new ArgumentException("source dir
doesn't exist -> " + srcdir);
}
//get all files in the current dir
files = dir.GetFiles();
//loop through each file
foreach(FileInfo file in files)
{
tmppath=Path.Combine(srcdir, file.Name);
if (file.LastWriteTime.AddDays(1) >= DateTime.Now)
{
Console.WriteLine(tmppath);
}
}
//cleanup
files = null;
dirs = dir.GetDirectories();
foreach(DirectoryInfo subdir in dirs)
{
tmppath = Path.Combine(srcdir,
subdir.Name);
//recursively call this function over and
over again with each new dir.
MyMethod(subdir.FullName);
}
//cleanup
dirs = null;
dir = null;
}// end method
/************************************************** *******
This overloaded method below is one way to handle the
fact that...Thread thread = new Thread(new ThreadStart
(MyMethod)) will not take parameters to the method call.
*******************************************/
private static void
MyMethod(string srcdir)
{
DirectoryInfo dir;
FileInfo[] files;
DirectoryInfo[] dirs;
string tmppath;
dir = new DirectoryInfo(srcdir);
if (! dir.Exists)
{
throw new ArgumentException("source dir
doesn't exist -> " + srcdir);
}
//get all files in the current dir
files = dir.GetFiles();
foreach(FileInfo file in files)
{
tmppath=Path.Combine(srcdir,file.Name);
if (file.LastWriteTime.AddDays(1) >= DateTime.Now)
{
Console.WriteLine(tmppath);
}
}
//cleanup
files = null;
dirs = dir.GetDirectories();
//loop through each sub directory in the
current dir
foreach(DirectoryInfo subdir in dirs)
{
tmppath = Path.Combine(srcdir,
subdir.Name);
//recursively call this function over and
over again with each new dir.
MyMethod(subdir.FullName);
}
dirs = null;
dir = null;
} //end method
} //end class