事件多级触发 然后程序就自动关闭 并且没有任何提示 怎么破?
代码如下 form里 有一个textbox 和一个 buttomform加载时 创建一个WatcherList(继承了ArrayList)的对象
按下button后出现文件夹选择 选择之后添加Watcher对象到 WatcherList中
Watcher利用FileSystemWatcher控件 可以监视刚才选择的文件夹下有没有东西被创建 比如新建一个txt
一旦创建了 就会触发控件的created事件 然后再触发Watcher的AddLog事件 WatcherList会接收到 并触发它的AddLog事件 然后form接收到 并改变textbox的值
执行之后 出现一旦创建了文件 程序就会在没有任何提示的情况下关闭 怎么破?
请各位大神帮忙呀 感激不尽
--------------------编程问答-------------------- 就算去掉那个WatcherList也一样 一触发就关闭
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.Collections;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private CatcherList l;
public Form1()
{
InitializeComponent();
l = new CatcherList();
l.AddLog += new EventHandler(l_AddLog);
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog f = new FolderBrowserDialog();
if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
l.Add(f.SelectedPath);
}
}
void l_AddLog(object sender, EventArgs e)
{
textBox1.Text += "!";
}
}
public class CatcherList : ArrayList
{
public event EventHandler AddLog;
public void OnAdd()
{
if (AddLog != null)
{
AddLog(this, new EventArgs());
}
}
public void Add(string path)
{
Catcher c = new Catcher(path);
c.AddLog += new EventHandler(c_AddLog);
base.Add(c);
}
void c_AddLog(object sender, EventArgs e)
{
OnAdd();
}
}
public class Catcher
{
public event EventHandler AddLog;
private FileSystemWatcher f;
public Catcher(string path)
{
f = new FileSystemWatcher(path);
f.Created += new FileSystemEventHandler(f_Created);
f.EnableRaisingEvents = true;
}
void f_Created(object sender, FileSystemEventArgs e)
{
OnAddLog();
MessageBox.Show("created");
}
public void OnAddLog()
{
if (AddLog != null)
{
AddLog(this, new EventArgs());
}
}
}
}
就是触发一个事件 然后在处理事件时候再触发另一个 这样不行么?
--------------------编程问答-------------------- 程序出现错误了
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog f = new FolderBrowserDialog();
if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Catcher c = new Catcher(f.SelectedPath);
c.AddLog += new EventHandler(c_AddLog);
}
}
void c_AddLog(object sender, EventArgs e)
{
textBox1.Text += "!";
}
}
public class Catcher
{
public event EventHandler AddLog;
private FileSystemWatcher f;
public Catcher(string path)
{
f = new FileSystemWatcher(path);
f.Created += new FileSystemEventHandler(f_Created);
f.EnableRaisingEvents = true;
}
void f_Created(object sender, FileSystemEventArgs e)
{
OnAddLog();
MessageBox.Show("created");
}
public void OnAddLog()
{
if (AddLog != null)
{
AddLog(this, new EventArgs());
}
}
}
异步线程操作空间了
使用invoke 跨线程操作 textBox1.Text += "!"控件 --------------------编程问答-------------------- 后台线程是不好碰前台控件的哦,需要这样:
MethodInvoker mi = delegate()
{
/// 要执行的代码
textBox1.Text += "!";
};
if (InvokeRequired)
Invoke(mi);
else
mi();
补充:.NET技术 , C#