完成后台工作从未解雇过

时间:2017-07-31 10:56:15

标签: c# backgroundworker invoke dispatcher

在后台进程DoWork()后,控件从块中退出,但它从不触发RunWorkerCompleted(),还有一件事是isBusy仍然是真的

请帮帮我

Dispatcher.CurrentDispatcher.Invoke(new Action(() =>            
    { 

 sendFile.Send();


}));

这是我的FileReadWrite.cs文件    每当我在xaml类文件中使用此文件时它都可以工作但是我在WindowBase.dll的DispatcherObject类中使用Dispatcher但在另一个.cs文件中我不能使用DispatcherObject类,这就是我使用Dispatcher的原因.CurrentDispatcher.Invoke()

public class FileReadWrite
        {
            public int SourceId;
            public int DestinationId;
            public string FileName;
            public WebSocket WebSocket;
            public int SplitSize;
            public int ReferenceId;
            BinaryWriter _binaryWriter;
            public int _totalsequence = 0;
            public int progvalue;
            public static double totallength;
            public static double calculate, sendlen;

            public static int post;
            public double calc, count, len, sentsize;

            public static int FileSendCount = 0;
            public static List<byte[]> FileList = new List<byte[]>();

            public BackgroundWorker _worker = new BackgroundWorker();

            public FileReadWrite()
            {
                _worker.DoWork += _worker_DoWork;
                _worker.RunWorkerCompleted += _worker_RunWorkerCompleted;
            }
            void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                FileCloseMessage fileclosemsg = new FileCloseMessage();
                fileclosemsg.Source = SourceId;
                fileclosemsg.Destination = DestinationId;
                fileclosemsg.Date = DateTime.Now;
                fileclosemsg.Sequenceno = 0;
                fileclosemsg.totalsequence = _totalsequence;
                byte[] bytess = fileclosemsg.GetBytes();
                FileList.Add(bytess);



            }
            void _worker_DoWork(object sender, DoWorkEventArgs e)
            {
                using (BinaryReader b = new BinaryReader(File.Open(FileName, FileMode.Open)))
                {
                    int pos = 0;
                    int length = (int)b.BaseStream.Length;
                    totallength = Convert.ToDouble(length);
                    byte[] bytes = new byte[SplitSize];

                    while (pos < length)
                    {
                        if (pos + SplitSize > length)
                        {
                            bytes = new byte[length - pos];
                        }


                        len = Convert.ToDouble(length);

                        bytes = b.ReadBytes(bytes.Length);
                        FileContentMesssage fileContentMesssage = new FileContentMesssage();
                        fileContentMesssage.Content = bytes;
                        fileContentMesssage.ReferenceId = ReferenceId;
                        pos += SplitSize;



                        fileContentMesssage.SequenceNo = pos / SplitSize;
                        _totalsequence++;
                        byte[] filebytes = fileContentMesssage.GetBytes();
                        FileList.Add(filebytes);

                    }

                }
            }
            public async void Send()
            {
               _worker.RunWorkerAsync();

            }
            public void FileReceive(byte[] bytes)
            {
                try
                {
                    if (_binaryWriter != null)
                        _binaryWriter.Write(bytes);
                }
                catch (Exception ex)
                {


                }
            }
            public string FileCreate()
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
                string filename = Path.GetFileName(FileName);
                string fullfilename = string.Format("{0}{1}", path, filename);
                _binaryWriter = new BinaryWriter(File.Open(fullfilename, FileMode.Create));
                return (fullfilename);
            }
            public void FileClose()
            {
                _binaryWriter.Close();

            }

            public byte[] FileSend()
            {

                byte[] bytess = FileList[FileSendCount];



                    FileSendCount++;

                return (bytess);

            }

            public void FileClosed()
            {

                FileSendCount = 0;
                FileList.Clear();
                post = 0;
                sendlen = 0;
                calculate = 0;
                totallength = 0;
                _totalsequence = 0;





}


        }

0 个答案:

没有答案
相关问题