I have a filewatcher that is watching a directory for file changes. When it happens, each file is queued in a Queue managed by a FileProcessor class (with Threads). I would like to update in a label which in the Form1 the number of files in the Q (everytime that number is updated) i tried several things including :
class FileProcessor : IDisposable
{
private static Form1 frm;
and the call later in the class :
frm.Invoke((MethodInvoker)delegate { frm.button4.Text = Qcounte(); });
I have a System.NullReferenceException because i do not know how to declare Form1 in frm :(
Here is the Main form with the function to update the label : SetQ(string s)
public delegate void ddisplayQ(string q);
public partial class Form1 : Form
{
public static ddisplayQ deldisplayQ;
public Form1()
{
InitializeComponent();
deldisplayQ = new ddisplayQ(this.setQ);
}
private void setQ(string s)
{
button4.Text = s;
}
on the FileProcessor class, here is how i'm calling the update of the label :
Form1.deldisplayQ.Invoke(Qcounte());
public string Qcounte()
{
return Convert.ToString(fileNamesQueue.Count);
}
Form1.deldisplayQ.Invoke(Qcounte());
Here is the whole class :
class FileProcessor : IDisposable
{
private EventWaitHandle eventWaitHandle = new AutoResetEvent(false);
private Thread worker;
private readonly object locker = new object();
private Queue<string> fileNamesQueue = new Queue<string>();
public static Form1 frm ;
public FileProcessor()
{
worker = new Thread(Work);
worker.Start();
}
public void EnqueueFileName(string FileName)
{
lock (locker) fileNamesQueue.Enqueue(FileName);
eventWaitHandle.Set();
}
private void Work()
{
while (true)
{
string fileName = null;
lock (locker)
if (fileNamesQueue.Count > 0)
{
if (fileName == null) return;
}
if (fileName != null)
{
ProcessFile(fileName);
}
else
{
eventWaitHandle.WaitOne();
}
}
}
private void ProcessFile(string FileName)
{
Globals.getTags(FileName);
Globals.ecritDb(FileName);
}
public string Qcounte()
{
return Convert.ToString(fileNamesQueue.Count);
}
#region IDisposable Members
public void Dispose()
{
EnqueueFileName(null);
worker.Join();
eventWaitHandle.Close();
}
#endregion
}
My question is : Where to trigger the enqueue and the dequeue in the FileProcessor class so that i can see the increase and the decrease of the file processing in my main form ? thanks in advance