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());//未将对象引用设置到对象的实例。
        }
    }
}

解决方案 »

  1.   

    DataOperFinished(new   object());//这里应该要求楼主传递一个对象实例了.
      

  2.   


    if(this.DataOperFinished != null)
        DataOperFinished(new   object());
      

  3.   

    1楼,你的说法不对。本来没有object这个要求,我是因为出了这个错误才加上的。结果还是一个样的错误。
    2楼,也不对。DataOperFinished就是null,事件不会被引发。是不是说事件引发的时候一定要求要在一个class里面?而在我的程序里因为是在独立的线程里缺少类?
      

  4.   

    错了,上面的DataOperFinished已经是在一个新类里了。但为什么DataOperFinished总是null呢?在同一个线程里不会出现这种情形啊。
      

  5.   

    因为你没有给事件DataOperFinished添加任何事件处理,和线程无关
      

  6.   

    DataOperFinished在哪做的代理?
    也就是DataOperFinished+=...
    我眼睛不好,請LZ明示
      

  7.   

    我的天哪!还以为在CSDN上发帖的都是强人呢............
    把下面代码加进去就好了.........
    public   FinishData()
    {
    DataOperFinished +=new FinishEventHandle(this.aaa);
    DataOperFinished(new   object());//未将对象引用设置到对象的实例。
    }
    public void aaa(object   obj)
    {
    }
      

  8.   

    贫僧最恨秃驴了:
    在fdT = new FinishData(byteToWritesss); 这句后面加上
    fdT.DataOperFinished += new FinishEventHandle(fdT_DataOperFinished);
    但是,不行。
    本来是没有这句的,但加上还是一样的错误。小白阿幻:
    你的方法怎么感觉怪怪的?
    在内部做事件处理虽然不会再出错,但外部的事件处理却不会被执行。
      

  9.   

    如果你研究过.NET事件处理的写法,你就不会对这个感到奇怪了.
    事实上我感觉.NET的事件定义类中也有这样的写法,将某个事件委托订阅到一个空方法里,然后在外部使用的时候继续+=一个事件.这样在触发这个事件后将首先进入定义类中的空方法,然后进入用户自定义的方法中.
      

  10.   

    小白阿幻:事实上还是引发不了外部的事件处理过程.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 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
      

  11.   

    你在外部设上以后根本就没有引发它。你设上之后得有地方执行DataOperFinished(new   object());的。
            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();
                    } 这样就是会有你要的结果的。
      

  12.   

    我自横刀香甜笑:你的方法是可以引发外部事件,谢谢了。我再好好看下是什么原理造成这种结果再来结贴吧。是不是原因就是这个?[quot]
    lye2000000_super 
    我自横刀香甜笑 
    等 级:
     发表于:2007-12-12 15:34:3411楼 得分:0 
    你不能在构造函数里就加上那个委托处理的,那时候还没有被赋初始值呢。所以你在外面加上那一句也还是会报错。至于在函数构造的时候直接加上委托,只是在内部作,不再外部作,所以不行的,你可以再设置一遍的。要么你就可以作为参数把委托的函数传进去就可以的。 
    [/quot]