Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CancellationToken works chaotically #649

Open
foxi69 opened this issue Jan 1, 2024 · 0 comments
Open

CancellationToken works chaotically #649

foxi69 opened this issue Jan 1, 2024 · 0 comments

Comments

@foxi69
Copy link

foxi69 commented Jan 1, 2024

Is it MonoTorrent error or what?

If "createTorrentsProgressForm" is closed, then "cancellationTokenSource.Cancel();" is called, and after this "Logger.LogToForm(mainForm, "cancelled");" (Work until this)

await torrentCreator.CreateAsync(fileSource, torrentFilePath, cancellationTokenSource.Token); - this should get this token and cancel, but nothing happend.

I am using 2.0.7.
Sometimes it works, sometimes it doesn't, same code, nothing changed.

Full code:

public static async Task<bool> CreateTorrentAsync(MainForm mainForm, CreateTorrentsProgressForm createTorrentsProgressForm, string path, string releaseName, int pieceLength = 0, string torrentComment = null, int counter = 0) {
            bool success = false;
            string torrentFilePath = string.Empty;
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            CancellationTokenRegistration cancellationTokenRegistration = new CancellationTokenRegistration();
            try {
                if (Directory.Exists(mainForm.torrentsFolder)) {
                    ITorrentFileSource fileSource = new TorrentFileSource(path, false);
                    torrentFilePath = Path.Combine(mainForm.torrentsFolder, fileSource.TorrentName) + StaticVariables.torrentExtension;
                    if (File.Exists(torrentFilePath)) {
                        File.Delete(torrentFilePath);
                    }
                    TorrentCreator torrentCreator = new TorrentCreator {
                        Private = true,
                        StoreMD5 = true
                    };
                    if (pieceLength > 0) {
                        torrentCreator.PieceLength = pieceLength * 1024;
                    }
                    if (!string.IsNullOrWhiteSpace(torrentComment)) {
                        torrentCreator.Comment = torrentComment;
                    }
                    createTorrentsProgressForm.FormClosing += (sender, e) => {
                        if (e.CloseReason == CloseReason.UserClosing) {
                            cancellationTokenSource.Cancel();
                        }
                    };
                    createTorrentsProgressForm.Show();
                    IProgress<int> progress = new Progress<int>(val => {
                        createTorrentsProgressForm.UpdateProgressBar(val);
                        createTorrentsProgressForm.Refresh();
                        if (val == 100 && counter == 0) {
                            createTorrentsProgressForm.Close();
                        }
                    });
                    cancellationTokenRegistration = cancellationTokenSource.Token.Register(() => {
                        Logger.LogToForm(mainForm, "cancelled");
                    });
                    torrentCreator.Hashed += (o, e) => {
                        int val = Math.Max((int)(e.OverallCompletion * 100), _overall);
                        if (val != _overall) {
                            _overall = val;
                            progress.Report(val);
                        }
                    };
                    createTorrentsProgressForm.UpdateLogTextBox("Creating torrent: " + releaseName + "...");
                    await torrentCreator.CreateAsync(fileSource, torrentFilePath, cancellationTokenSource.Token);
                    success = true;
                }
            }
            catch (Exception ex) {
                Logger.ErrorLogToFile(ex, MethodBase.GetCurrentMethod().Name);
                success = false;
            }
            finally {
                cancellationTokenRegistration.Dispose();
                if (!cancellationTokenSource.Token.IsCancellationRequested && File.Exists(torrentFilePath)) {
                    mainForm.torrentFilesCreated.Add(torrentFilePath);
                }
                if (!createTorrentsProgressForm.IsDisposed && createTorrentsProgressForm != null) {
                    createTorrentsProgressForm.UpdateLogTextBox("Torrent create has been finished: " + releaseName);
                    if (counter == 0 || cancellationTokenSource.Token.IsCancellationRequested) {
                        createTorrentsProgressForm.Close();
                    }
                }
            }
            return success;
        }
@foxi69 foxi69 changed the title CancellationToken error CancellationToken works chaotically Jan 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant