using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace MoveFile
{
public partial class frmMain : Form
{
WriteBytes wrT; public frmMain()
{
InitializeComponent();
} private void frmMain_Load(object sender, EventArgs e)
{
wrT = new WriteBytes(null);
wrT.WriteData(new byte[1024]);
}
} public class WriteBytes
{
System.IO.BinaryWriter bwWriter;
byte[] byteToWritesss;
FinishData fdT; public WriteBytes(System.IO.BinaryWriter bwDest)
{
bwWriter = bwDest;
} public void WriteData(byte[] byteToWrite)
{
byteToWritesss = byteToWrite;
System.Threading.Thread thrT = new System.Threading.Thread(new System.Threading.ThreadStart(Write));
thrT.Start();
} private void Write()
{
for (Int32 I = 0; I < byteToWritesss.Length; I++)
{
System.Console.WriteLine(I);
}
fdT = new FinishData(byteToWritesss);
}
} public delegate void FinishEventHandle(object obj); public class FinishData
{
public event FinishEventHandle DataOperFinished; public FinishData(byte[] byteS)
{
DataOperFinished(new object());//未将对象引用设置到对象的实例。
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace MoveFile
{
public partial class frmMain : Form
{
WriteBytes wrT; public frmMain()
{
InitializeComponent();
} private void frmMain_Load(object sender, EventArgs e)
{
wrT = new WriteBytes(null);
wrT.WriteData(new byte[1024]);
}
} public class WriteBytes
{
System.IO.BinaryWriter bwWriter;
byte[] byteToWritesss;
FinishData fdT; public WriteBytes(System.IO.BinaryWriter bwDest)
{
bwWriter = bwDest;
} public void WriteData(byte[] byteToWrite)
{
byteToWritesss = byteToWrite;
System.Threading.Thread thrT = new System.Threading.Thread(new System.Threading.ThreadStart(Write));
thrT.Start();
} private void Write()
{
for (Int32 I = 0; I < byteToWritesss.Length; I++)
{
System.Console.WriteLine(I);
}
fdT = new FinishData(byteToWritesss);
}
} public delegate void FinishEventHandle(object obj); public class FinishData
{
public event FinishEventHandle DataOperFinished; public FinishData(byte[] byteS)
{
DataOperFinished(new object());//未将对象引用设置到对象的实例。
}
}
}
if(this.DataOperFinished != null)
DataOperFinished(new object());
2楼,也不对。DataOperFinished就是null,事件不会被引发。是不是说事件引发的时候一定要求要在一个class里面?而在我的程序里因为是在独立的线程里缺少类?
也就是DataOperFinished+=...
我眼睛不好,請LZ明示
把下面代码加进去就好了.........
public FinishData()
{
DataOperFinished +=new FinishEventHandle(this.aaa);
DataOperFinished(new object());//未将对象引用设置到对象的实例。
}
public void aaa(object obj)
{
}
在fdT = new FinishData(byteToWritesss); 这句后面加上
fdT.DataOperFinished += new FinishEventHandle(fdT_DataOperFinished);
但是,不行。
本来是没有这句的,但加上还是一样的错误。小白阿幻:
你的方法怎么感觉怪怪的?
在内部做事件处理虽然不会再出错,但外部的事件处理却不会被执行。
事实上我感觉.NET的事件定义类中也有这样的写法,将某个事件委托订阅到一个空方法里,然后在外部使用的时候继续+=一个事件.这样在触发这个事件后将首先进入定义类中的空方法,然后进入用户自定义的方法中.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace MoveFile
{
public partial class frmMain : Form
{
WriteBytes wrT; public frmMain()
{
InitializeComponent();
} private void btFile_Click(object sender, EventArgs e)
{
dlgFile.ShowDialog();
} private void btFolder_Click(object sender, EventArgs e)
{ } private void dlgFile_FileOk(object sender, CancelEventArgs e)
{
tbFile.Text = string.Join(ConstantVars.sSeperator, dlgFile.FileNames);
wrT = new WriteBytes(null);
wrT.WriteData(new byte[1024]);
} private void frmMain_Load(object sender, EventArgs e)
{
wrT = new WriteBytes(null);
wrT.WriteData(new byte[1024]);
}
} public class WriteBytes
{
System.IO.BinaryWriter bwWriter;
byte[] byteToWritesss;
FinishData fdT; public WriteBytes(System.IO.BinaryWriter bwDest)
{
bwWriter = bwDest;
} public void WriteData(byte[] byteToWrite)
{
byteToWritesss = byteToWrite;
System.Threading.Thread thrT = new System.Threading.Thread(new System.Threading.ThreadStart(Write));
thrT.Start();
} private void Write()
{
for (Int32 I = 0; I < byteToWritesss.Length; I++)
{
System.Console.WriteLine(I);
}
fdT = new FinishData(byteToWritesss);
fdT.DataOperFinished += new FinishEventHandle(fdT_DataOperFinished);
} void fdT_DataOperFinished(object obj)//这个过程不会被执行
{
throw new Exception("The method or operation is not implemented.");
} } public delegate void FinishEventHandle(object obj); public class FinishData
{
public event FinishEventHandle DataOperFinished; public FinishData(byte[] byteS)
{
DataOperFinished += new FinishEventHandle(FinishData_DataOperFinished);
DataOperFinished(new object());//未将对象引用设置到对象的实例。
} public void FinishData_DataOperFinished(object obj)
{
System.Console.WriteLine("FinishData_DataOperFinished");
}
}
}输出:
0
1
2
FinishData_DataOperFinished
public class FinishData
{
public event FinishEventHandle DataOperFinished; public FinishData(byte[] byteS)
{
DataOperFinished += new FinishEventHandle(FinishData_DataOperFinished);
DataOperFinished(new object());//未将对象引用设置到对象的实例。
} public void FinishData_DataOperFinished(object obj)
{
System.Console.WriteLine("FinishData_DataOperFinished");
}
public void Test()
{
if(DataOperFinished != null){ DataOperFinished(new object()); }
}
}
private void Write()
{
for (Int32 I = 0; I < byteToWritesss.Length; I++)
{
System.Console.WriteLine(I);
}
fdT = new FinishData(byteToWritesss);
fdT.DataOperFinished += new FinishEventHandle(fdT_DataOperFinished);
fdT.Test();
} 这样就是会有你要的结果的。
lye2000000_super
我自横刀香甜笑
等 级:
发表于:2007-12-12 15:34:3411楼 得分:0
你不能在构造函数里就加上那个委托处理的,那时候还没有被赋初始值呢。所以你在外面加上那一句也还是会报错。至于在函数构造的时候直接加上委托,只是在内部作,不再外部作,所以不行的,你可以再设置一遍的。要么你就可以作为参数把委托的函数传进去就可以的。
[/quot]