简体   繁体   中英

async button event sometimes doesn't work

namespace knowledge
{
    public partial class FrmFtpr : Form
    {

    private readonly ILog _log = LogManager.GetLogger("Ftp");
        private CancellationTokenSource  _cancellationTokenSource;

    private IEnumerable<FtpHost> GetFtpHost()
        {
          //get all ftp site info

            return ftpHost;
        }

        private async Task DoWork()
        {
            while (!_cancellationTokenSource.IsCancellationRequested)
            {
                var ftpHosts = GetFtpHosts();
                var ftpTasks = ftpHosts.Select(Upload);
                await Task.WhenAll(ftpTasks); 
            }
        }

        private async Task Upload(FtpHost ftpHost)
        {
            //upload files to a ftp
        }


        private async void btnStart_Click(object sender, EventArgs e)
        {
            _cancellationTokenSource = new CancellationTokenSource();

            _log.Info(" Started");
            btnCancel.Enabled = true;
            btnStart.Enabled = false;
            await DoWork();
        }

        private async void btnCancel_Click_1(object sender, EventArgs e)
        {
            _cancellationTokenSource.Cancel();
            _log.Info(" Stoped");
            btnStart.Enabled = true;
            btnCancel.Enabled = false;
        }
    }

}

above code is used to monitor some directories, and upload files in those directories to different ftps.

If GetFtpHost return null or 0, form will freeze,but if GetFtpHost return some value, I can switch between start and cancel button. the winform not freeze.

By modified code like below the issue has been resolved. now this application can scan dirs period(every 1 minute for example)and then if current upload not complete, wait. Otherwise start a new round.

public partial class FrmFtpr : Form
{
    private readonly ILog _log = LogManager.GetLogger("Ftp");
    private CancellationTokenSource  _cancellationTokenSource;
    private Task _task;

    private IEnumerable<FtpHost> GetFtpHost()
    {
      //get all ftp site info

        return ftpHost;
    }


    private async Task Upload(FtpHost ftpHost)
    {
        //upload files to a ftp
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (_task != null && !_task.IsCompleted)
            return;

        var ftpTargets = GetFtpTargets().ToList();

        if (ftpTargets.Count == 0)
            return;

        _task = Task.Factory.StartNew(() =>
            {
               var tasks = ftpTargets.Select(Upload).ToArray();
                Task.WaitAll(tasks);
            });
    }


    private async void btnStart_Click(object sender, EventArgs e)
    {
        _cancellationTokenSource = new CancellationTokenSource();
        _log.Info(" Started");
        btnCancel.Enabled = true;
        btnStart.Enabled = false;
        timer1.Start();
    }

    private async void btnCancel_Click_1(object sender, EventArgs e)
    {
        _cancellationTokenSource.Cancel();
        _task.Wait();
        _log.Info(" Stoped");
        timer1.Stop();
        btnStart.Enabled = true;
        btnCancel.Enabled = false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM