参数传递到事件处理程序 [英] passing parameter to an event handler

查看:111
本文介绍了参数传递到事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过我的列表<串> 作为使用我的事件参数

 公共事件的EventHandler _newFileEventHandler; 
名单,LT;字符串> _filesList =新的List<串GT;();

公共无效startListener(字符串目录路径)
{
FileSystemWatcher的守望者=新FileSystemWatcher的(目录);
_filesList =新的List<串GT;();
_timer =新System.Timers.Timer(5000);
watcher.Filter =* .pcap;
watcher.Created + = watcher_Created;
watcher.EnableRaisingEvents = TRUE;
watcher.IncludeSubdirectories = TRUE;
}

无效watcher_Created(对象发件人,FileSystemEventArgs E)
{
_timer.Elapsed + =新ElapsedEventHandler(myEvent);
_timer.Enabled = TRUE;
_filesList.Add(e.FullPath);
_fileToAdd = e.FullPath;
}

私人无效myEvent(对象发件人,ElapsedEventArgs E)
{
_newFileEventHandler(_filesList,EventArgs.Empty);;
}

和从我的主要形式,我想获得这个列表:



 无效listener_newFileEventHandler(对象发件人,EventArgs五)
{

}


解决方案

请新EventArgs类,如:

 公共类ListEventArgs:EventArgs的
{
公开名单<串GT;数据{搞定;组; }
公共ListEventArgs(列表<串GT;数据)
{
数据=数据;
}
}

和使您的活动是这样的:

 公共事件的EventHandler< ListEventArgs> NewFileAdded; 

添加烧制方法:

 保护无效OnNewFileAdded(列表<串GT;数据)
{
VAR localCopy = NewFileAdded;
如果(localCopy!= NULL)
{
localCopy(这一点,新ListEventArgs(数据));
}
}



当你要处理此事件:

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

的处理方法似乎是这样的:

 公共无效myObj_NewFileAdded(对象发件人,ListEventArgs E)
{
//你想用什么e.Data(这是字符串的列表)
}


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)
{

}

解决方案

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)
}

这篇关于参数传递到事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆