示例#1
0
 public XunleiClient(string fileName, string link)
 {
     _fileName = fileName;
     _stParam  = new Xl.DownTaskParam
     {
         szTaskUrl  = link,
         szSavePath = Path.Combine(Environment.CurrentDirectory, "ErrorSongs"),
         szFilename = DateTime.Now.Ticks + CommonHelper.GetFormat(fileName)
     };
     _taskIntPtr = Xl.XL_CreateTask(_stParam);
     Xl.XL_StartTask(_taskIntPtr);
 }
示例#2
0
        public Task DownloadFileTaskAsync()
        {
            var tcs = new TaskCompletionSource <bool>();

            new Timer(self =>
            {
                Xl.DownTaskInfo downTaskInfo = new Xl.DownTaskInfo();
                Xl.XL_QueryTaskInfoEx(_taskIntPtr, downTaskInfo);
                var isError = false;
                switch (downTaskInfo.stat)
                {
                case Xl.DownTaskStatus.TscDownload:
                    _progress = Convert.ToInt32(downTaskInfo.fPercent * 100);
                    break;

                case Xl.DownTaskStatus.TscComplete:
                    ((IDisposable)self).Dispose();
                    _progress   = 100;
                    var oriPath = Path.Combine(Environment.CurrentDirectory, "ErrorSongs", _stParam.szFilename);
                    if (File.Exists(_fileName))
                    {
                        var newFileName = _fileName.Replace(CommonHelper.GetFormat(_fileName),
                                                            new Random(DateTime.Now.Millisecond).Next(0, 1000) + CommonHelper.GetFormat(_fileName));
                        File.Move(_fileName, newFileName);
                    }
                    File.Move(oriPath, _fileName);
                    tcs.TrySetResult(true);
                    break;

                case Xl.DownTaskStatus.TscStartpending:
                    _progress = 0;
                    break;

                default:
                    isError   = true;
                    _progress = 100;
                    ((IDisposable)self).Dispose();
                    tcs.TrySetResult(true);
                    break;
                }
                XunleiProgressChanged?.Invoke(null, new ProgressChangedEventArgs(_progress, isError));
            }).Change(0, 500);
            return(tcs.Task);
        }