Example #1
0
 public void CopyToBoard()
 {
     CopyThread = new Thread(new ThreadStart(delegate() {
         while (CopyToStream.Position < CopyToStream.Length - 1)
         {
             PartFileInfo xx = new PartFileInfo()
             {
                 fileName   = FileSafeName,
                 Index      = CopyToStream.Position,
                 FileLength = CopyToStream.Length
             };
             xx.PartFileLength = CopyToStream.Read(xx.data, 0, PartFileInfo.BufferSize);
             PartFileInfo.CopyToClipBoard(xx);
             CopyProgressGo?.Invoke(this, new ProgressInfo()
             {
                 value = xx.Index, max = xx.FileLength
             });
             Thread.Sleep(Interval);
         }
         CopyProgressGo?.Invoke(this, new ProgressInfo()
         {
             value = 1, max = 1
         });
     }));
     CopyThread.SetApartmentState(ApartmentState.STA);
     //thread.IsBackground = true;
     CopyThread.Start();
 }
Example #2
0
        public void WriteToFile()
        {
            Thread GetFromBoard = new Thread(new ThreadStart(delegate()
            {
                string str = Clipboard.GetText();
                while (true)
                {
                    if (str != lastString)
                    {
                        fromBoard.Enqueue(str);
                        lastString = str;
                    }
                    str = Clipboard.GetText();
                }
            }));

            GetFromBoard.SetApartmentState(ApartmentState.STA);
            GetFromBoard.Start();
            ReadThread = new Thread(new ThreadStart(delegate()
            {
                Clipboard.SetText("123");
                //string str= Clipboard.GetText();
                string str;
                fromBoard.TryDequeue(out str);
                var partFile = PartFileInfo.Prase(str);
                while (true)
                {
                    if (partFile.Check())
                    {
                        if (first)
                        {
                            first         = false;
                            WriteToStream = File.Create(Path + "//" + partFile.fileName);
                        }

                        if (FileBufferList.Count == bufferlength)
                        {
                            FileBufferList.ForEach(p =>
                            {
                                WriteToStream.Write(p.data, 0, (int)p.PartFileLength);
                            });
                            FileBufferList.Clear();
                        }

                        if (FileBufferList.Count == 0 && partFile.Index == 0)
                        {
                            FileBufferList.Add(partFile);
                            last = partFile;
                            ReadProgressGo?.Invoke(this, new ProgressInfo()
                            {
                                value = 0,
                                max   = partFile.FileLength
                            });
                        }
                        else
                        {
                            if (last.Index + last.PartFileLength == partFile.Index)
                            {
                                FileBufferList.Add(partFile);
                                last = partFile;
                                ReadProgressGo?.Invoke(this, new ProgressInfo()
                                {
                                    value = partFile.Index,
                                    max   = partFile.FileLength
                                });

                                if (partFile.Index + partFile.PartFileLength == partFile.FileLength)
                                {
                                    FileBufferList.ForEach(p =>
                                    {
                                        WriteToStream.Write(p.data, 0, (int)p.PartFileLength);
                                    });
                                    FileBufferList.Clear();
                                    break;
                                }
                            }
                            else
                            {
                                if (last.Index + last.PartFileLength < partFile.Index)
                                {
                                    FileLost?.Invoke(this, new PartFileLost()
                                    {
                                        Index  = last.Index + last.PartFileLength,
                                        length = partFile.Index - (last.Index + last.PartFileLength)
                                    });
                                }
                                //else
                                //    throw new Exception("文件传输错误,建议重新传输");
                            }
                        }
                    }
                    fromBoard.TryDequeue(out str);
                    partFile = PartFileInfo.Prase(str);
                }
                WriteToStream.Flush();
                WriteToStream.Close();
                if (GetFromBoard != null && GetFromBoard.IsAlive)
                {
                    GetFromBoard.Abort();
                }
                //文件传输完毕
                ReadProgressGo?.Invoke(this, new ProgressInfo()
                {
                    value = 1,
                    max   = 1
                });
            }));
            ReadThread.IsBackground = true;
            ReadThread.SetApartmentState(ApartmentState.STA);
            ReadThread.Start();
        }
Example #3
0
        public static void CopyToClipBoard(PartFileInfo pile)
        {
            string temp = pile.ToString();

            Clipboard.SetText(temp);
        }