C# threading question
- From: "j" <mr.jawright@xxxxxxxxx>
- Date: 2 Feb 2007 12:43:37 -0800
i am trying to implement something similar to this: http://
www.thescripts.com/forum/post1737333-5.html in C#.
my "worker thread" is running, debugging statements that i've added to
the top of the "processfiles" function have shown that. But, the
_files_ are not getting _processed_. I've not had opportunity to work
much with threading, so I'm sure there's some low-hanging fruit that
will fix it. I've included my code.
Hopefully, google won't futz with the formatting too much.
Any help/advice will be greatly appreciated.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Threading;
using System.Collections;
using ImageServices;
namespace ImageService
{
public class FileSystemMonitor
{
RegistryKey _root;
Object _debugkey, _archiveDirectory;
private volatile Queue _qEvents;
private volatile bool _bStopThreads;
private System.Diagnostics.EventLog eventLog1;
public FileSystemMonitor()
{
_qEvents = Queue.Synchronized(new Queue());
// TODO: Add code here to start your service.
this.eventLog1 = new System.Diagnostics.EventLog();
((System.ComponentModel.ISupportInitialize)
(this.eventLog1)).BeginInit();
this.eventLog1.Log = "Application";
this.eventLog1.Source = "ImageService";
eventLog1.MachineName = System.Environment.MachineName;
_root = Registry.LocalMachine.OpenSubKey("SOFTWARE",
true);
_root = _root.CreateSubKey("ImageService");
_debugkey = _root.GetValue("Debug");
if (_debugkey == null)
{
_root.SetValue("Debug", "0");
_debugkey = 0;
}
eventLog1.WriteEntry(_debugkey + ":Debugging is " +
((Convert.ToInt32(_debugkey) == 1) ? "On" : "Off"),
EventLogEntryType.Information, 2500);
_archiveDirectory = _root.GetValue("Archive");
if (_archiveDirectory == null)
{
_root.SetValue("Archive", @"C:\archive\");
_archiveDirectory = @"C:\archive\";
}
if (!Convert.ToString(_archiveDirectory).EndsWith(@"\"))
{
_archiveDirectory =
Convert.ToString(_archiveDirectory) + @"\";
}
eventLog1.WriteEntry("Archive:" +
Convert.ToString(_archiveDirectory), EventLogEntryType.Information,
2500);
_bStopThreads = false;
if (Convert.ToInt32(_debugkey) == 1)
{
eventLog1.WriteEntry("FileSystemMonitor Created",
EventLogEntryType.Information, 2500);
}
}
public void ProcessFiles()
{
while ((!_bStopThreads) && (_qEvents.Count!=0)){
try
{
// if (_qEvents.Count > 0)
lock(_qEvents.SyncRoot)
{
Debug.Assert(false);
FileSystemEventArgs e =
(FileSystemEventArgs)_qEvents.Dequeue();
FileInfo f = new FileInfo(e.FullPath);
if (Convert.ToInt32(_debugkey) == 1)
{
eventLog1.WriteEntry("Processing
"+e.FullPath, EventLogEntryType.Information, 2500);
}
//the file watcher throws this even on file
create...not after the file is created...
//so, we loop until the file open stops
throwing IO exceptions.
bool x = true;
while (x)
{
try
{
FileStream _fs =
File.OpenRead(e.FullPath);
while (!_fs.CanRead) { x = true; }
if (_fs.CanRead) { x = false; }
_fs.Close();
}
catch (IOException)
{
x = true;
}
}
string ext = f.Extension.ToLower();
if ((ext == ".jpg") || (ext == ".jpeg"))//||
(ext == ".tif") || (ext == ".tiff"))
{
f.CopyTo(Convert.ToString(_archiveDirectory) + f.Name, true);
Photo ir = new Photo(f);
if (ir.ResizePhoto())
{
if (Convert.ToInt32(_debugkey) == 1)
{
eventLog1.WriteEntry(f.FullName +
" was resized", EventLogEntryType.Information, 2500);
}
}
else
{
if (Convert.ToInt32(_debugkey) == 1)
{
eventLog1.WriteEntry(f.FullName +
" was not resized", EventLogEntryType.Information, 2500);
}
}
}
}
}
catch (UnauthorizedAccessException uae)
{
eventLog1.WriteEntry(uae.Message + "\n" +
uae.InnerException, EventLogEntryType.Error, 3000);
}
catch (ArgumentNullException ane)
{
eventLog1.WriteEntry(ane.Message + "\n" +
ane.InnerException, EventLogEntryType.Error, 3000);
}
catch (DirectoryNotFoundException dnfe)
{
eventLog1.WriteEntry(dnfe.Message + "\n" +
dnfe.InnerException, EventLogEntryType.Error, 3000);
}
catch (FileNotFoundException fnfe)
{
eventLog1.WriteEntry(fnfe.Message + "\n" +
fnfe.InnerException, EventLogEntryType.Error, 3000);
}
catch (PathTooLongException ptle)
{
eventLog1.WriteEntry(ptle.Message + "\n" +
ptle.InnerException, EventLogEntryType.Error, 3000);
}
catch (NotSupportedException nse)
{
eventLog1.WriteEntry(nse.Message + "\n" +
nse.InnerException, EventLogEntryType.Error, 3000);
}
catch (IOException ioe)
{
eventLog1.WriteEntry(ioe.Message + "\n" +
ioe.InnerException, EventLogEntryType.Error, 3000);
}
catch (System.Security.SecurityException se)
{
eventLog1.WriteEntry(se.Message + "\n" +
se.InnerException, EventLogEntryType.Error, 3000);
}
catch (ArgumentException ae)
{
eventLog1.WriteEntry(ae.Message + "\n" +
ae.InnerException, EventLogEntryType.Error, 3000);
}
catch (System.Runtime.InteropServices.COMException CE)
{
eventLog1.WriteEntry("HResult:" + CE.ErrorCode +
"\n" + CE.Message + "\n" + CE.InnerException, EventLogEntryType.Error,
3000);
}
catch (Exception ex)
{
eventLog1.WriteEntry(ex.Message + "\n" +
ex.InnerException, EventLogEntryType.Error, 3000);
}
}
}
public void ReqThreadStop()
{
_bStopThreads = true;
}
public void AddNewEvent(FileSystemEventArgs e)
{
_qEvents.Enqueue(e);
eventLog1.WriteEntry(_qEvents.Count+ " events in queue",
EventLogEntryType.Error, 2500);
}
}
public partial class Service : ServiceBase
{
FileSystemMonitor watcher;
RegistryKey _root;
Object _dirkey;
Thread watcherThread;
public Service()
{
InitializeComponent();
watcher = new FileSystemMonitor();
}
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
_root = Registry.LocalMachine.OpenSubKey("SOFTWARE",
true);
_root = _root.CreateSubKey("ImageService");
_dirkey = _root.GetValue("Directory");
if (_dirkey == null)
{
_dirkey = "\\\\" + System.Environment.MachineName + "\
\compliance\\blob";
_root.SetValue("Directory", _dirkey);
}
fileSystemWatcher1.Path = Convert.ToString(_dirkey);
eventLog1.WriteEntry("Watching:" + fileSystemWatcher1.Path
+ " for " + fileSystemWatcher1.Filter, EventLogEntryType.Information,
1000);
eventLog1.MachineName = System.Environment.MachineName;
eventLog1.WriteEntry("ImageService Start",
EventLogEntryType.Information, 1000);
Photo p = new Photo();
eventLog1.WriteEntry("FileSystemMonitor Thread Start",
EventLogEntryType.Information, 1000);
watcherThread = new Thread(watcher.ProcessFiles);
watcherThread.Start();
while (!watcherThread.IsAlive) ;
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary
to stop your service.
watcher.ReqThreadStop();
watcherThread.Join();
eventLog1.WriteEntry("ImageService Stop",
EventLogEntryType.Information, 1001);
}
private void fileSystemWatcher1_Created(object sender,
FileSystemEventArgs e)
{
eventLog1.WriteEntry("fileSystemWatcher1_Created event
fired for" + e.FullPath, EventLogEntryType.Information, 2000);
watcher.AddNewEvent(e);
Thread.Sleep(1);
}
}
}
.
- Prev by Date: axWebBrowser + CSS
- Next by Date: byte endianess
- Previous by thread: axWebBrowser + CSS
- Next by thread: byte endianess
- Index(es):