21

i want to pass my List<string> as parameter using my event

public event EventHandler _newFileEventHandler;
    List<string> _filesList = new List<string>();

public void startListener(string directoryPath)
{
    FileSystemWatcher watcher = new FileSystemWatcher(directoryPath);
    _filesList = new List<string>();
    _timer = new System.Timers.Timer(5000);
    watcher.Filter = "*.pcap";
    watcher.Created += watcher_Created;            
    watcher.EnableRaisingEvents = true;
    watcher.IncludeSubdirectories = true;
}

void watcher_Created(object sender, FileSystemEventArgs e)
{            
    _timer.Elapsed += new ElapsedEventHandler(myEvent);
    _timer.Enabled = true;
    _filesList.Add(e.FullPath);
    _fileToAdd = e.FullPath;
}

private void myEvent(object sender, ElapsedEventArgs e)
{
    _newFileEventHandler(_filesList, EventArgs.Empty);;
}

and from my main form i want to get this List:

void listener_newFileEventHandler(object sender, EventArgs e)
{

}
user1269592
  • 691
  • 3
  • 12
  • 24

2 Answers2

69

Make a new EventArgs class such as:

    public class ListEventArgs : EventArgs
    {
        public List<string> Data { get; set; }
        public ListEventArgs(List<string> data)
        {
            Data = data;
        }
    }

And make your event as this:

    public event EventHandler<ListEventArgs> NewFileAdded;

Add a firing method:

protected void OnNewFileAdded(List<string> data)
{
    var localCopy = NewFileAdded;
    if (localCopy != null)
    {
        localCopy(this, new ListEventArgs(data));
    }
}

And when you want to handle this event:

myObj.NewFileAdded += new EventHandler<ListEventArgs>(myObj_NewFileAdded);

The handler method would appear like this:

public void myObj_NewFileAdded(object sender, ListEventArgs e)
{
       // Do what you want with e.Data (It is a List of string)
}
Saw
  • 6,199
  • 11
  • 53
  • 104
  • 2
    Just as a side note, consider inheriting from EventArgs (or other suitable class in EventArgs hierarchy) – James Michael Hare Dec 27 '12 at 17:13
  • in my class i am listening to folder using FileSystemWatcher and after new file arrived i am add this file to my List and than fired my event so should i allready have event (public event EventHandler _newFileEventHandler;), see my update – user1269592 Dec 27 '12 at 17:20
  • I know, you should change your definition to public event EventHandler _newFileEventHandler; after adding the new "ListEventArgs" class (Note: it should inherit from EventArgs class) – Saw Dec 27 '12 at 17:22
  • why i should change it ? – user1269592 Dec 27 '12 at 17:24
  • I have changed the names to something like your case :) the only change you need is to change the event handler of your event, you shouldn't name your event xxxxxEventHandler, it is not fine, because the event handler is something else, I have named the event: FileAdded – Saw Dec 27 '12 at 17:27
  • 1
    I made a small change to how your `OnNewFileAdded` function works. If you have a multi-threaded application and the last caller unsubscribes from the event in between the null check and the delegate call you will get a `NullRefrenceException`. By copying the delegate in to a temporary variable first it prevents this from happening. – Scott Chamberlain Dec 27 '12 at 17:48
7

You can define the signature of the event to be whatever you want. If the only information the event needs to provide is that list, then just pass that list:

public event Action<List<string>> MyEvent;

private void Foo()
{
     MyEvent(new List<string>(){"a", "b", "c"});
}

Then when subscribing to the event:

public void MyEventHandler(List<string> list)
{
    //...
}
Servy
  • 202,030
  • 26
  • 332
  • 449