Convert BackgroundWorker to task in SendSpace

This commit is contained in:
Jaex 2020-09-13 07:24:06 +03:00
parent 374245b7fa
commit a75b6deb27

View file

@ -27,11 +27,11 @@ You should have received a copy of the GNU General Public License
using ShareX.UploadersLib.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
@ -531,26 +531,26 @@ public UploadResult Upload(Stream stream, string fileName, UploadInfo uploadInfo
public class CheckProgress : IDisposable
{
private SendSpace sendSpace;
private BackgroundWorker bw;
private string url;
private int interval = 1000;
private CancellationTokenSource cts;
public CheckProgress(string progressURL, SendSpace sendSpace)
{
url = progressURL;
this.sendSpace = sendSpace;
bw = new BackgroundWorker { WorkerSupportsCancellation = true };
bw.DoWork += bw_DoWork;
bw.RunWorkerAsync();
cts = new CancellationTokenSource();
Task.Run(() => DoWork(cts.Token), cts.Token);
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
private void DoWork(CancellationToken ct)
{
Thread.Sleep(1000);
ProgressInfo progressInfo = new ProgressInfo();
int progress, elapsed;
DateTime time;
while (!bw.CancellationPending)
while (!ct.IsCancellationRequested)
{
time = DateTime.Now;
try
@ -620,7 +620,10 @@ public override string ToString()
public void Dispose()
{
bw.CancelAsync();
if (cts != null)
{
cts.Cancel();
}
}
}