110,571
社区成员
发帖
与我相关
我的任务
分享
foreach (KeyValuePair<int, Pictures> list in pictureList)//遍历字典生成图片
{
if (GenTecPic(list.Value.datapath, list.Value.macropath, 0))
{
FileSystemWatcher watcher = new FileSystemWatcher();//设置监视器
watcher.Path = list.Value.datapath;
watcher.Changed += new FileSystemEventHandler(TecDat_Changed);
watcher.Created += new FileSystemEventHandler(TecDat_Changed);
watcher.EnableRaisingEvents = true;
list.Value.watcher = watcher;
}
}
timer_tecplot.Enabled = true;
void TecDat_Changed(object sender, FileSystemEventArgs e)
{
}
foreach (KeyValuePair<int, Pictures> list in pictureList)//遍历字典生成图片
{
if (GenTecPic(list.Value.datapath, list.Value.macropath, 0))
{
FileSystemWatcher watcher = new FileSystemWatcher();//设置监视器
watcher.Path = list.Value.datapath;
Pictures pic = new Pictures();
pic.datapath = list.Value.datapath;
pic.macropath = list.Value.macropath;
watcher.Changed+=new FileSystemEventHandler(pic.TecDat_Changed);
//watcher.Changed += delegate(object s, FileSystemEventArgs ev)
//{
// UpdateMacro(list.Value.datapath, ev.Name, list.Value.macropath);
// RunPython(list.Value.macropath);
//};
watcher.EnableRaisingEvents = true;
list.Value.watcher = watcher;
}
}
好吧,最后用这个原始的方法给解决了。
[code=csharp]
foreach (KeyValuePair<int, Pictures> list in pictureList)//遍历字典生成图片
{
if (GenTecPic(list.Value.datapath, list.Value.macropath, 0))
{
FileSystemWatcher watcher = new FileSystemWatcher();//设置监视器
watcher.Path = list.Value.datapath;
Pictures pic = new Pictures();
pic.datapath = list.Value.datapath;
pic.macropath = list.Value.macropath;
watcher.Changed+=new FileSystemEventHandler(pic.TecDat_Changed);
//watcher.Changed += delegate(object s, FileSystemEventArgs ev)
//{
// UpdateMacro(list.Value.datapath, ev.Name, list.Value.macropath);
// RunPython(list.Value.macropath);
//};
watcher.EnableRaisingEvents = true;
list.Value.watcher = watcher;
}
}
public class Pictures
{
public string macropath;//宏的路径
public string datapath;//数据的目录
public string jpegpath;//生成图片存放的路径
public FileSystemWatcher watcher;//对应的监视器
public Pictures()
{
}
public Pictures(string m,string d,string j)
{
this.macropath = m;
this.datapath = d;
this.jpegpath = j;
}
public void TecDat_Changed(object sender, FileSystemEventArgs e)
{
UpdateMacro(datapath, e.Name, macropath);
RunPython(macropath);
}
}
不过这样写确实很麻烦,不知道那个问题怎么能在匿名方法中解决?还希望能解答一下,好多学习学习,谢谢啦~
foreach (KeyValuePair<int, Pictures> list in pictureList)//遍历字典生成图片
{
if (GenTecPic(list.Value.datapath, list.Value.macropath, 0))
{
FileSystemWatcher watcher = new FileSystemWatcher();//设置监视器
watcher.Path = list.Value.datapath;
watcher.Changed += delegate(object s, FileSystemEventArgs ev)
{
UpdateMacro(list.Value.datapath, ev.Name, list.Value.macropath);
RunPython(list.Value.macropath);
};
watcher.Created += delegate(object s, FileSystemEventArgs ev)
{
UpdateMacro(list.Value.datapath, ev.Name, list.Value.macropath);
RunPython(list.Value.macropath);
};
watcher.EnableRaisingEvents = true;
list.Value.watcher = watcher;
}
}
假设我现在pictureList里有两个数据:
(1)pictureList[0]:datapath:E:\project\bin\Debug\data
(2)pictureList[1]:datapath:I:\GenJPEG\bin\Debug\data
现在我手动在E:\project\bin\Debug\data里面增加文件,打断点,我发现监视器里的list.Value.datapath却是I:\GenJPEG\bin\Debug\data,list.Value.macropath的值亦pictureList[1]里的,这怎么才能解决啊???
这是Pictures类的定义:
public class Pictures
{
public string macropath;//宏的路径
public string datapath;//数据的目录
public string jpegpath;//生成图片存放的路径
public FileSystemWatcher watcher;//对应的监视器
public Pictures()
{
}
public Pictures(string m,string d,string j)
{
this.macropath = m;
this.datapath = d;
this.jpegpath = j;
}
}
这是字典的定义:
Dictionary<int, Pictures> pictureList = new Dictionary<int, Pictures>();
watcher.Changed += (s,e) =>
{
这里直接写事件处理代码,可以直接调用 list.Value.macropath!
}
看看匿名委托中引用外部变量的说明吧!
例如你可以写
if (GenTecPic(list.Value.datapath, list.Value.macropath, 0))
{
FileSystemWatcher watcher = new FileSystemWatcher();//设置监视器
watcher.Path = list.Value.datapath;
watcher.Changed += (s,e) =>
{
OnWaterChanged(list.Value.datapath);
}
......
也就是说在匿名方法中,你可以调用事先写好的另外一个方法,将来 Changed 事件触发时再把 list.Value.datapath 直接作为参数传给这个方法。public class MytecProxy
{
public string path;
......以及其它任何必要的参数
public void TecDat_Changed(object sender, FileSystemEventArgs e)
{
//在这里,你就可以访问到所有参数
}
}
然后调用时写MyTecProxy obj = new MyTecProxy();
obj.path = list.Value.macropath;
watcher.Changed += new FileSystemEventHandler(obj.TecDat_Changed);
这样,当事件处理方法执行时,你通过 obj.xxxx 赋值的参数全都可以取到。
在c#3.0中(好像是这个版本)出现了非常优雅的“匿名委托”写法,其中可以引用方法之前定义的变量。这就不用你自己去定义 MyTecProxy 类型了。你可以直接写[code=csharp]watcher.Changed += (s,e) =>
{
这里直接写事件处理代码,可以直接调用 list.Value.macropath!
}code]
因为匿名委托,会自动创建类似于 MyTecProxy 的内部类型,会自动将委托方法中使用到的外部变量编译为这个注册方法的过程的一部分。
匿名委托非常优雅!