Tell fileSystemWatcher to wait - c#

I have some files that are written to a folder.First there are 2 files written and after 10-20 minutes the next 2 files.
My question is:
Is there any possible way to tell the file system watcher
to wait until all 4 files are in the folder before executing my code?

According to #BugFinder 's suggestion I created something similar but didn't test. Hope it is useful:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace CustomFileWatcher
{
public class CustomFileWatcher : IDisposable
{
private FileSystemWatcher fileWatcher;
private IList<string> fileList;
private IList<string> createdFiles;
public event EventHandler FilesCreated;
protected void OnFilesCreated(EventArgs e)
{
var handler = FilesCreated;
if (handler != null)
handler(this, e);
}
public CustomFileWatcher(IList<string> waitForTheseFiles, string path)
{
fileList = waitForTheseFiles;
createdFiles = new List<string>();
fileWatcher = new FileSystemWatcher(path);
fileWatcher.Created += fileWatcher_Created;
}
void fileWatcher_Created(object sender, FileSystemEventArgs e)
{
foreach (var item in fileList)
{
if (fileList.Contains(e.Name))
{
if (!createdFiles.Contains(e.Name))
{
createdFiles.Add(e.Name);
}
}
}
if (createdFiles.SequenceEqual(fileList))
OnFilesCreated(new EventArgs());
}
public CustomFileWatcher(IList<string> waitForTheseFiles, string path, string filter)
{
fileList = waitForTheseFiles;
createdFiles = new List<string>();
fileWatcher = new FileSystemWatcher(path, filter);
fileWatcher.Created += fileWatcher_Created;
}
public void Dispose()
{
if (fileWatcher != null)
fileWatcher.Dispose();
}
}
}
Usage
class Program
{
static void Main(string[] args)
{
IList<string> waitForAllTheseFilesToBeCopied = new List<string>();
waitForAllTheseFilesToBeCopied.Add("File1.txt");
waitForAllTheseFilesToBeCopied.Add("File2.txt");
waitForAllTheseFilesToBeCopied.Add("File3.txt");
string watchPath = #"C:\OutputFolder\";
CustomFileWatcher customWatcher = new CustomFileWatcher(waitForAllTheseFilesToBeCopied, watchPath);
customWatcher.FilesCreated += customWatcher_FilesCreated;
}
static void customWatcher_FilesCreated(object sender, EventArgs e)
{
// All files created.
}
}

Related

How do I remove treeviewitem using MVVM pattern?

I'm making a file explorer app using MVVM pattern in C# WPF. Right now I want to implement watcher events that are responsible for adding, removing and renaming items from treeview. I already have adding, and partially renaming (I think that in this case I have to combine deleting and adding). I'm struggling with deletion.
Deleted files are stil in the treeview. For example, Folder shouldn't exist anymore, because I deleted it.
App window
I would very much appreciate help from You. Here's a code I've written in class DirectoryInfoViewModel
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
namespace ViewModels {
public class DirectoryInfoViewModel : FileSystemInfoViewModel
{
public ObservableCollection<FileSystemInfoViewModel> Items { get; private set; } = new ObservableCollection<FileSystemInfoViewModel>();
public bool Open(string path)
{
bool result = false;
try
{
FileSystemWatcher Watcher = new FileSystemWatcher(path);
Watcher.Created += OnFileCreated;
Watcher.Renamed += OnFileRenamed;
Watcher.Deleted += OnFileDeleted;
//Watcher.Changed += OnFileChanged;
Watcher.Error += Watcher_Error;
Watcher.EnableRaisingEvents = true;
foreach (var dirName in Directory.GetDirectories(path))
{
var dirInfo = new DirectoryInfo(dirName);
DirectoryInfoViewModel itemViewModel = new DirectoryInfoViewModel
{
Model = dirInfo
};
itemViewModel.Open(dirName);
Items.Add(itemViewModel);
}
foreach (var fileName in Directory.GetFiles(path))
{
var fileInfo = new FileInfo(fileName);
FileInfoViewModel itemViewModel = new FileInfoViewModel();
itemViewModel.Model = fileInfo;
Items.Add(itemViewModel);
}
result = true;
}
catch (Exception ex)
{
Exception = ex;
}
return result;
}
public Exception Exception { get; private set; }
private static void Watcher_Error(object sender, ErrorEventArgs e) =>
System.Windows.MessageBox.Show(e.GetException().ToString());
public void OnFileCreated(object sender, FileSystemEventArgs e)
{
System.Windows.Application.Current.Dispatcher.Invoke(() => OnFileCreated(e));
}
private void OnFileCreated(FileSystemEventArgs e)
{
Debug.WriteLine("File Created: " + e.Name);
if (!Items.Any(x => x.Caption == e.Name))
{
var dirInfo = new DirectoryInfo(e.FullPath);
DirectoryInfoViewModel itemViewModel = new DirectoryInfoViewModel();
itemViewModel.Model = dirInfo;
Items.Add(itemViewModel);
}
}
public void OnFileDeleted(object sender, FileSystemEventArgs e)
{
System.Windows.Application.Current.Dispatcher.Invoke(() => OnFileDeleted(e));
}
private void OnFileDeleted(FileSystemEventArgs e)
{
Debug.WriteLine("File Deleted: " + e.Name);
if (Items.Any(x => x.Caption == e.Name))
{
var dirInfo = new DirectoryInfo(e.FullPath);
Debug.WriteLine("File path: " + e.FullPath);
DirectoryInfoViewModel itemViewModel = new DirectoryInfoViewModel();
itemViewModel.Model = dirInfo;
Items.Remove(itemViewModel);
}
}
public void OnFileRenamed(object sender, FileSystemEventArgs e)
{
System.Windows.Application.Current.Dispatcher.Invoke(() => OnFileRenamed(e));
}
private void OnFileRenamed(FileSystemEventArgs e)
{
Debug.WriteLine("File Renamed: " + e.Name);
OnFileDeleted(e);
OnFileCreated(e);
}
} }
In private void OnFileDeleted(FileSystemEventArgs e) you create a new DirectoryInfoViewModel and attempt to remove it from the Items collection. ObservableCollection<T>.Remove(T) removes item based on a reference equality check if T does not provide an alternative equality check.
So you attempt to remove an item from for you Items collection which is not in the collection. If you check the return value of Items.Remove(itemViewModel) you will see that it returns false as no item equal to itemViewModel was found in the collection (see Collection.Remove(T)).
This would fix the problem by finding the item you want to remove and then removing it.
private void OnFileDeleted(FileSystemEventArgs e)
{
if (Items.Any(x => x.Caption == e.Name))
{
var toDelete = Items.Single(x => x.Caption == e.Name);
Items.Remove(toDelete);
}
}

FileSystemWatcher resetting EnableRaisingEvents after invocation

I need to process a number of directories and process files as they are created or renamed. To do this I created the following Watcher Class.
public class Watcher
{
FileSystemWatcher fileSystemWatcher = null;
public delegate void FileCreatedEventHandler(object sender, FileSystemEventArgs e);
public delegate void FileRenamedEventHandler(object sender, RenamedEventArgs e);
public delegate void LogEventHandler(object sender, String e);
public event FileCreatedEventHandler FileCreated;
public event FileRenamedEventHandler FileRenamed;
public event LogEventHandler LogEvent;
public String Directory { get; set; }
public String DAP { get; set; }
public String Filter { get; set; }
public Watcher()
{
fileSystemWatcher = new FileSystemWatcher();
}
public Watcher(String directory, String filter, String dap)
{
this.DAP = dap;
this.Directory = directory;
this.Filter = filter;
}
public void StartWatch()
{
if (fileSystemWatcher == null) fileSystemWatcher = new FileSystemWatcher();
fileSystemWatcher.Filter = Filter;
fileSystemWatcher.Path = Directory;
fileSystemWatcher.EnableRaisingEvents = true;
fileSystemWatcher.Created += FileSystemWatcher_Created;
fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
Log(String.Format("Watching Directory {0}", Directory));
}
private void FileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
{
FileRenamed?.Invoke(sender, e);
}
private void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
FileCreated?.Invoke(sender, e);
}
private void Log ( String Message )
{
LogEvent?.Invoke(this, Message);
}
}
That in turn is used by the following function, which sets up a number of instances; one for each of the directories being watched.
public ProfileWatcher()
{
List<nhs_acquisition_profile> p = null;
List <Watcher> fwList = new List<Watcher>();
InitializeComponent();
try
{
if (!System.Diagnostics.EventLog.SourceExists("DirectoryScanner"))
{
System.Diagnostics.EventLog.CreateEventSource(
"DirectoryScanner","ProfileDirectoryScanner");
}
//
profileWatcherLog.Source = "DirectoryScanner";
profileWatcherLog.Log = "ProfileDirectoryScanner";
using (nhs_acquisition_profiles profiles = new nhs_acquisition_profiles(Properties.Settings.Default.DataConnection))
{
profileWatcherLog.WriteEntry("retrieving list of DAP Profiles");
p = profiles.Select<nhs_acquisition_profile>(null);
profileWatcherLog.WriteEntry(String.Format("Found {0} Directories to watch.", p.Count.ToString()));
foreach (nhs_acquisition_profile pfl in p)
{
Watcher w = null;
try
{
profileWatcherLog.WriteEntry(String.Format("Attempting to set-up watcher on {0} for DAP {1}",pfl.dap_file_location,pfl.dap_name));
w = new Watcher(pfl.dap_file_location, "*.*", pfl.dap_name);
profileWatcherLog.WriteEntry("Initialising Event Handlers");
// initialise event handlers
w.FileCreated += W_FileCreated;
w.FileRenamed += W_FileRenamed;
w.LogEvent += W_LogEvent;
profileWatcherLog.WriteEntry("Event Handlers initialised");
w.StartWatch();
profileWatcherLog.WriteEntry("Watch started....Adding to Watcher List");
// add the watcher to the list of watchers
fwList.Add(w);
profileWatcherLog.WriteEntry("Added to list of file watchers");
profileWatcherLog.WriteEntry(String.Format("Watching {0} for files matching *.* for DAP {1}",pfl.dap_file_location,pfl.dap_name));
}
catch
{
throw;
}
}
}
}
catch (Exception e)
{
profileWatcherLog.WriteEntry(String.Format("ERROR: {0}",e.Message),EventLogEntryType.Error);
}
finally
{
profileWatcherLog.WriteEntry("Profile Watcher setup");
}
}
Each time a file is created it should call one of the following
private void W_FileRenamed(object sender, RenamedEventArgs e)
{
try
{
profileWatcherLog.WriteEntry(String.Format("File Renamed From {0} to {1}", e.OldName, e.Name));
using (FileSystemWatcher watcher = ((System.IO.FileSystemWatcher)sender))
{
if (watcher != null)
Process(watcher.Path);
}
}
catch (System.Exception ex)
{
profileWatcherLog.WriteEntry(ex.Message, EventLogEntryType.Error);
}
finally
{
}
}
private void W_FileCreated(object sender, FileSystemEventArgs e)
{
try
{
profileWatcherLog.WriteEntry(String.Format("File Created {0}", e.Name));
using (FileSystemWatcher watcher = ((System.IO.FileSystemWatcher)sender))
{
if (watcher != null)
Process(watcher.Path);
}
}
catch ( System.Exception ex)
{
profileWatcherLog.WriteEntry(ex.Message, EventLogEntryType.Error);
}
finally
{
}
}
This is fine for the first time a file is dropped into a target directory. The trouble is it is resetting the EnableRaisingEvents once it returns from the
FileCreated?.Invoke(sender, e);
Line in the first listing above.
Nothing in the code seems to be doing this and when I try and reset it back after the FileCreated it then moans that the object has been disposed (even though the debugger shows it has a value)
The whole thing is then wrapped in a service, so that it runs all the time.

SerialPort C# custom delegate from DataReceived

I'm doing a class for working with the serial port.
It's all going quiet until the item to receive a data through the serial port, the class raise an event in the main application.
My question is: how to pass parameters to a delegate and use it in my class because my class is so independent.
Below the sources and where I like to spend delegates.
Class control serial port:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace TCCExterna.Lib
{
public class PortaSerial //: IDisposable
{
private SerialPort serialPort;
private Queue<byte> recievedData = new Queue<byte>();
public PortaSerial()
{
serialPort = new SerialPort();
serialPort.DataReceived += serialPort_DataReceived;
}
public void Abrir(string porta, int velocidade)
{
serialPort.PortName = porta;
serialPort.BaudRate = velocidade;
serialPort.Open();
}
public string[] GetPortas()
{
return SerialPort.GetPortNames();
}
public string[] GetVelocidades()
{
return new string[] { "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200" };
}
void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e)
{
byte[] data = new byte[serialPort.BytesToRead];
serialPort.Read(data, 0, data.Length);
data.ToList().ForEach(b => recievedData.Enqueue(b));
processData();
// like this use LineReceivedEvent or LineReceived
}
private void processData()
{
// Determine if we have a "packet" in the queue
if (recievedData.Count > 50)
{
var packet = Enumerable.Range(0, 50).Select(i => recievedData.Dequeue());
}
}
public void Dispose()
{
if (serialPort != null)
serialPort.Dispose();
}
}
}
Program:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using TCCExterna.Lib;
namespace TCCExterna
{
public partial class FormPrincipal : Form
{
PortaSerial sp1 = new PortaSerial(); // like this command passed LineReceivedEvent or LineReceived
public delegate void LineReceivedEvent(string line);
public void LineReceived(string line)
{
//What to do with the received line here
}
public FormPrincipal()
{
InitializeComponent();
cmbPortas.Items.AddRange(sp1.GetPortas());
cmbVelocidade.Items.AddRange(sp1.GetVelocidades());
}
}
}
If I got it clearly, what you want is this: (se quiser pode explicar melhor em português, depois a gente traduz pro site).
//delcare an event args clas
public class LineReceivedEventArgs : EventArgs
{
//Data to pass to the event
public string LineData{get; private set;}
public LineReceivedEventArgs(string lineData)
{
this.LineData = lineData
}
}
//declare a delegate
public delegate void LineReceivedEventHandler(object sender, LineReceivedEventArgs Args);
public class PortaSerial //: IDisposable
{
private SerialPort serialPort;
private Queue<byte> recievedData = new Queue<byte>();
//add event to class
public event LineReceivedEventHandler LineReceived;
public PortaSerial()
{
serialPort = new SerialPort();
serialPort.DataReceived += serialPort_DataReceived;
}
public void Abrir(string porta, int velocidade)
{
serialPort.PortName = porta;
serialPort.BaudRate = velocidade;
serialPort.Open();
}
public string[] GetPortas()
{
return SerialPort.GetPortNames();
}
public string[] GetVelocidades()
{
return new string[] { "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200" };
}
void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e)
{
byte[] data = new byte[serialPort.BytesToRead];
serialPort.Read(data, 0, data.Length);
data.ToList().ForEach(b => recievedData.Enqueue(b));
processData();
//raise event here
if (this.LineReceived != null)
LineReceived(this, new LineReceivedEventArgs("some line data"));
}
private void processData()
{
// Determine if we have a "packet" in the queue
if (recievedData.Count > 50)
{
var packet = Enumerable.Range(0, 50).Select(i => recievedData.Dequeue());
}
}
public void Dispose()
{
if (serialPort != null)
serialPort.Dispose();
}
}
public partial class FormPrincipal : Form
{
PortaSerial sp1 = new PortaSerial(); // like this command passed LineReceivedEvent or LineReceived
// event handler method
void sp1_LineReceived(object sender, LineReceivedEventArgs Args)
{
//do things with line
MessageBox.ShowDialog(Args.LineData);
}
public FormPrincipal()
{
InitializeComponent();
//add handler to event
sp1.LineReceived += new LineReceivedEventHandler(sp1_LineReceived);
cmbPortas.Items.AddRange(sp1.GetPortas());
cmbVelocidade.Items.AddRange(sp1.GetVelocidades());
}
}

BackgroundWorker report progress from external Class?

I have a working solution that reports progress & text to a progress bar and a label on the applications's main form. I have now moved my worker methods to a class to they are accessible across multiple forms etc.
Within the worker methods are BW.ReportProgress() statements that push back the progress and text to the BackgroundWorker in the main form.
To give a better idea here is the file layout:
MainScreen.cs
List repSelected = new List();
XMLandRar xXMLandRar = new XMLandRar();
private void Rarbtn_Click(object sender, EventArgs e)
{
GetReps();
//Run worker
if (!CreateRarBW.IsBusy)
{
CreateRarBW.RunWorkerAsync();
}
}
//Worker
private void CreateRarBW_DoWork(object sender, DoWorkEventArgs e)
{
xXMLandRar.RarFiles(repSelected);
}
//Progress reporting
private void CreateRarBW_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progBar.Value = e.ProgressPercentage;
Statuslbl.Text = e.UserState.ToString();
}
Then my newly created Class that encompasses all of the worker methods and is to push progress to the main form.
XMLandRar.cs
public class XMLandRar
{
public void RarFiles(List repSelected)
{
int step = 100 / repSelected.Count();
int i = 0;
//Iterate through list and run rar for each
foreach (string rep in repSelected)
{
CreateRarBW.ReportProgress(i, "Raring files for " + rep);
DirectoryExists(rep);
ProcessRunner(rep);
i += step;
CreateRarBW.ReportProgress(i, "Raring files for " + rep);
}
}
}
The problem I am having is that in the XMLandRar class the CreateRarBW is not recognised (obviously) - how can I make a ReportProgress call to the BW in the main screen of the application?
Create an event in your XMLandRar class which you could subscribe to.
This way the XMLandRar class doesn't need to know or care about the UI or progressbar, it only cares about sending a message if anyone would listen. And there can also be more than one subscriber (let's say if you want to report to the background worker and a log, maybe)
Example:
private void Rarbtn_Click(object sender, EventArgs e)
{
GetReps();
//Run worker
if (!CreateRarBW.IsBusy)
{
// This should be done once, maybe in the contructor. Bind to new event.
xXMLandRar.ReportProgress += new EventHandler<XMLandRar.ProgressArgs>(xXMLandRar_ReportProgress);
CreateRarBW.RunWorkerAsync();
}
}
protected void xXMLandRar_ReportProgress(object sender, XMLandRar.ProgressArgs e)
{
// Call the UI backgroundworker
CreateRarBW.ReportProgress(e.Percentage, e.Message);
}
public class XMLandRar
{
// Event handler to bind to for reporting progress
public EventHandler<ProgressArgs> ReportProgress;
// Eventargs to contain information to send to the subscriber
public class ProgressArgs : EventArgs
{
public int Percentage { get; set; }
public string Message { get; set; }
}
public void RarFiles(List repSelected)
{
int step = 100 / repSelected.Count();
int i = 0;
//Iterate through list and run rar for each
foreach (string rep in repSelected)
{
// Report progress if somebody is listening (subscribed)
if (ReportProgress != null)
{
ReportProgress(this, new ProgressArgs { Percentage = i, Message = "Raring files for " + rep });
}
DirectoryExists(rep);
ProcessRunner(rep);
i += step;
// Report progress if somebody is listening (subscribed)
if (ReportProgress != null)
{
ReportProgress(this, new ProgressArgs { Percentage = i, Message = "Raring files for " + rep });
}
}
}
}
The sender object in the DoWork callback is the BackgroundWorker instance which is calling this callback.
This enables to use the instance and add it to your new XMLandRar class.
private void CreateRarBW_DoWork(object sender, DoWorkEventArgs e)
{
var worker = sender as BackgroundWorker.
xXMLandRar.RarFiles(repSelected, worker);
}
XMLandRar.cs
public class XMLandRar
{
public void RarFiles(List repSelected, BackgroundWorker worker)
{
// ...
}
}
Or you set the BackgroundWorkerinstance as a class property to the XMLandRar.
public class XMLandRar
{
protected BackgroundWorker mWorker;
public XMLandRar(BackgroundWorker worker) {
mWorker = BackgroundWorker;
}
public void RarFiles(List repSelected)
{
// Do something with {mWorker}
}
}
Or as mentioned in the comments, using events in the XMLandRar class.
XMLandRar.cs
public class XmlandRarCompletedEventArgs : EventArgs
{
public readonly bool Finished;
public readonly bool Canceled;
public XmlandRarCompletedEventArgs(bool finished)
{
Finished = finished;
Canceled = !finished;
}
}public class OnXmlandRarUpdateEventArgs : EventArgs
{
public readonly int Percentage;
public readonly string Message;
public XmlandRarCompletedEventArgs(int perc) :
this(perc, "") {
}
public XmlandRarCompletedEventArgs(int perc, string message)
{
Percentage = perc;
Message = message;
}
}
public delegate void OnXmlandRarDoWorkHandler(object o);
public delegate void OnXmlandRarUpdateHandler(object o, OnXmlandRarUpdateEventArgs args);
public delegate void OnXmlandRarCompleteHandler(object o, XmlandRarCompletedEventArgs args);
public class XMLandRar
{
public event OnXmlandRarDoWorkHandler OnDoWork;
public event OnXmlandRarUpdateHandler OnUpdate;
public event OnXmlandRarCompletedHandler OnComplete;
public void RarFiles(List repSelected)
{
TriggerDoWork();
int step = 100 / repSelected.Count();
int i = 0;
//Iterate through list and run rar for each
foreach (string rep in repSelected)
{
TriggerUpdate(i, "Raring files for " + rep);
DirectoryExists(rep);
ProcessRunner(rep);
i += step;
TriggerUpdate(i, "Raring files for " + rep);
}
TriggerComplete(true);
}
private void TriggerDoWork()
{
if (OnDoWork != null) {
OnDoWork(this);
}
}
private void TriggerUpdate(perc) {
}
if (OnUpdate != null) {
OnUpdate(this, new OnXmlandRarUpdateEventArgs(perc));
}
private void TriggerUpdate(perc, string message)
{
if (OnUpdate != null) {
OnUpdate(this, new OnXmlandRarUpdateEventArgs(perc, message));
}
}
private void TriggerComplete(bool finished)
{
if (OnComplete != null) {
OnComplete(this, new XmlandRarCompletedEventArgs(finished));
}
}
}
Usage:
private void CreateRarBW_DoWork(object sender, DoWorkEventArgs e)
{
var worker = sender as BackgroundWorker;
// Attach events to class
xXMLandRar.OnDoWork += delegate(object o) {
// ...
};
xXMLandRar.OnUpdate += delegate(object o, OnXmlandRarUpdateEventArgs args) {
// ...
};
xXMLandRar.OnComplete += delegate(object o, XmlandRarCompletedEventArgs args) {
// ...
};
xXMLandRar.RarFiles(repSelected, worker);
}
Hopefully without typos 'cause I'm in the office.
I have fixed errors in the code submitted and cleaned it up... This is a working sample that will help those that maybe couldn't understand the code since it was broken as it was... Although I would like to say that the intent and functionality of the code after it was cleaned up and enhanced is excellent.
This is working code that can get you started in your project for using a backGroundWorker thread for whatever you need.
Just modify this method -
public void RarFiles(List<string> repSelected)
To do whatever work you need. You will also have to modify the arguments you plan on using.. i.e. you may need a DataTable or some custom object...
You can modify the
public class OnXmlandRarUpdateEventArgs : EventArgs
For your needs.. that way when you get a callback.. you can update your main UI form with the changes made to those items..
You may need to do some tweaking.. but you see what i mean..
This is the Form Code.. Don't forget to create a button on the form...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ThreadSample
{
public partial class Form1 : Form
{
List<string> repSelected = new List<string>();
XMLandRar xXMLandRar = new XMLandRar();
BackgroundWorker CreateRarBW = new BackgroundWorker();
public Form1()
{
InitializeComponent();
repSelected = new List<string> { "asdf", "asdfsd", "h;ljj" };
CreateRarBW.DoWork += new DoWorkEventHandler(CreateRarBW_DoWork);
}
private void Rarbtn_Click(object sender, EventArgs e)
{
//GetReps();
//Run worker
if (!CreateRarBW.IsBusy)
{
// This should be done once, maybe in the contructor. Bind to new event.
xXMLandRar.ReportProgress += new EventHandler<XMLandRar.ProgressArgs>(xXMLandRar_ReportProgress);
CreateRarBW.RunWorkerAsync();
}
}
protected void xXMLandRar_ReportProgress(object sender, XMLandRar.ProgressArgs e)
{
// Call the UI backgroundworker
CreateRarBW.ReportProgress(e.Percentage, e.Message);
}
//private void CreateRarBW_DoWork(object sender, DoWorkEventArgs e)
//{
// var worker = sender as BackgroundWorker;
// xXMLandRar.RarFiles(repSelected, worker);
//}
private void CreateRarBW_DoWork(object sender, DoWorkEventArgs e)
{
var worker = sender as BackgroundWorker;
// Attach events to class
xXMLandRar.OnDoWork += delegate(object o)
{
// ...
MessageBox.Show("Hey ... Something is going on over there in the classLib .. " + o);
};
xXMLandRar.OnUpdate += delegate(object o, OnXmlandRarUpdateEventArgs args)
{
// ...
//foreach (object oo in args)
{
MessageBox.Show("Hey ... Something is going on over there in the classLib .. Message is " + args.Message + " and Percentage is " + args.Percentage);
}
};
xXMLandRar.OnComplete += delegate(object o, XmlandRarCompletedEventArgs args)
{
MessageBox.Show("Hey ... Something is going on over there in the classLib .. Canceled is " + args.Canceled + " and Finished is " + args.Finished);
// ...
};
xXMLandRar.RarFiles(repSelected);//, worker);
}
}
}
This is the class code. You can just create a class in your current project... Keep in mind that the CreateRarBW object is a BackGroundWorker instance... (Wasn't included above..)
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace ThreadSample
{
public class XmlandRarCompletedEventArgs : EventArgs
{
public readonly bool Finished;
public readonly bool Canceled;
public XmlandRarCompletedEventArgs(bool finished)
{
Finished = finished;
Canceled = !finished;
}
}
public class OnXmlandRarUpdateEventArgs : EventArgs
{
public readonly int Percentage;
public readonly string Message;
public OnXmlandRarUpdateEventArgs(int perc) : this(perc, "")
{
}
public OnXmlandRarUpdateEventArgs(int perc, string message)
{
Percentage = perc;
Message = message;
}
}
public delegate void OnXmlandRarDoWorkHandler(object o);
public delegate void OnXmlandRarUpdateHandler(object o, OnXmlandRarUpdateEventArgs args);
public delegate void OnXmlandRarCompleteHandler(object o, XmlandRarCompletedEventArgs args);
public class XMLandRar // : BackgroundWorker
{
// Event handler to bind to for reporting progress
public EventHandler<ProgressArgs> ReportProgress;
// Eventargs to contain information to send to the subscriber
public class ProgressArgs : EventArgs
{
public int Percentage { get; set; }
public string Message { get; set; }
}
public event OnXmlandRarDoWorkHandler OnDoWork;
public event OnXmlandRarUpdateHandler OnUpdate;
public event OnXmlandRarCompleteHandler OnComplete;
public void RarFiles(List<string> repSelected)
{
TriggerDoWork();
int step = 100 / repSelected.Count();
int i = 0;
//Iterate through list and run rar for each
foreach (string rep in repSelected)
{
TriggerUpdate(i, "Raring files for " + rep);
//DirectoryExists(rep);
//ProcessRunner(rep);
i += step;
TriggerUpdate(i, "Raring files for " + rep);
}
TriggerComplete(true);
}
private void TriggerDoWork()
{
if (OnDoWork != null)
{
OnDoWork(this);
}
}
private void TriggerUpdate(int perc)
{
if (OnUpdate != null)
{
OnUpdate(this, new OnXmlandRarUpdateEventArgs(perc));
}
}
private void TriggerUpdate(int perc, string message)
{
if (OnUpdate != null)
{
OnUpdate(this, new OnXmlandRarUpdateEventArgs(perc, message));
}
}
private void TriggerComplete(bool finished)
{
if (OnComplete != null)
{
OnComplete(this, new XmlandRarCompletedEventArgs(finished));
}
}
}
}

Any efficient/reliable way to expose one event?

Any efficient/reliable way to expose one event?
I have a class, MultipleDocumentCopier that copies multiple documents thru an instance of SingleDocumentCopier.
SingleDocumentCopier exposes an event CopyCompleted that is fired when a file is copied.
Suppose that, I am copying 10 files, instead of raising SingleDocumentCopier.CopyCompleted 10 times,
I would like to expose an event, MultipleDocumentCopier.MultipleCopyCompleted.
But is there a standard way/technique to combine multiple events and fire it once?
I would like to raise MultipleDocumentCopier.MultipleCopyCompleted only once
within 'MultipleDocumentCopier.singleDocumentCopier_CopyCompleted', instead of 10 times.
Here is the sample code
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace CombineEvents
{
public class Program
{
public static void Main(string[] args)
{
var copier = new MultipleDocumentCopier();
copier.MultipleCopyCompleted += MultipleDocumentCopyCompleted;
copier.CopyDocuments(new[] {"File1", "File2", "File3"});
}
private static void MultipleDocumentCopyCompleted(
object sender, FileNameEventArgs e)
{
Debug.Print("Following documents have been copied");
foreach (var fileName in e.FileNames)
{
Debug.Print("\t\t\"{0}\"", fileName);
}
}
}
internal class SingleDocumentCopier
{
public event EventHandler CopyCompleted;
protected virtual void OnCopyCompleted()
{
if (CopyCompleted != null) CopyCompleted(this, EventArgs.Empty);
}
public void Copy(string fileName)
{
Debug.Print("Copying = '{0}'", fileName);
OnCopyCompleted();
}
}
public class MultipleDocumentCopier
{
public event EventHandler<FileNameEventArgs> MultipleCopyCompleted;
protected virtual void OnCopyCompleted(FileNameEventArgs e)
{
EventHandler<FileNameEventArgs> completed = MultipleCopyCompleted;
if (completed != null) completed(this, e);
}
public void CopyDocuments(IEnumerable<string> fileNames)
{
var copier = new SingleDocumentCopier();
copier.CopyCompleted += singleDocumentCopier_CopyCompleted;
foreach (var fileName in fileNames)
{
copier.Copy(fileName);
}
}
public static void singleDocumentCopier_CopyCompleted(
object sender, EventArgs e)
{
// I want to raise "MultipleDocumentCopier.MultipleCopyCompleted" when
// all files, `fileNames` in "CopyDocuments" have been copied,
// not for every file being copied.
}
}
public class FileNameEventArgs : EventArgs
{
private readonly List<string> _FileNames;
public List<string> FileNames
{
get { return _FileNames; }
}
public FileNameEventArgs(IEnumerable<string> fileNames)
{
_FileNames = fileNames.ToList();
}
}
}
Why not call MultipleDocumentCopier.OnCopyCompleted from the end of CopyDocuments, and forget singleDocumentCopier_CopyCompleted entirely?
Or maybe this is pseudocode, and your real code is more complicated? Maybe you could keep a collection of outstanding file names inside MultipleDocumentCopier, and each time the singleDocumentCopier_CopyCompleted is raised, you remove one document from the collection. Once the collection becomes empty you call MultipleDocumentCopier.OnCopyCompleted.
Edit: Re 'is there a standard way?' -- not that I'm aware of in C#; F# has an interesting set of mechanisms for combining events like this, but I assume a change in programming language isn't an option.

Categories