question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Command.Wait() inside BackgroundWorker.DoWork() kill the app

See original GitHub issue

Hi, I try to run several Command and it works fine. the problem is i’d like to report progress in a winform using BackgroundWorker for respectful syncing on the UI thread. But the app crash without respect for any try catch at first Command.Wait() during debug.

Any idea ?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
hguycommented, Sep 5, 2022

This solution did work for a time and it start crashing again. I give up :p There is some random weirdness i can’t comprehend in this and it’s not that necessary. Thanks for your help though.

0reactions
hguycommented, Sep 5, 2022

After my various refacto i didn’t reproduce the crash but i finaly succeed.

This construct by doing a mix of BackgroundWorker and IProgress do work.

Inside GitAddCommitTagAndPush() i use the BindingList<> hook from previous post.

I ask IProgress to call backgroundWorker.ReportProgress() so my underlying layers are agnostic and doesn’t know System.Windows.Form.

Thank you for your help!


	/// <summary>
	/// Handler for closing the main form
	/// </summary>
	/// <param name="sender">sender object</param>
	/// <param name="e">CancelEventArgs data</param>
	private void MainFormClosing(object sender, CancelEventArgs e)
	{
		if (!_DoCloseStuffCompleted)
		{
			e.Cancel = !this.DoCloseStuff();
			if (e.Cancel) return;// Problem ? no need to go further

			if (Config.UserSettings.Default.GitBackupEnabled)
			{
				var pb = this.vaultProgressBar;
				pb.BringToFront();
				pb.Minimum =
				pb.Value = 0;
				pb.Maximum = 100;
				pb.TitleForeColor = TQColor.Purple.Color();
				pb.TitleFont = FontService.GetFont(15F, this.UIService.Scale);

				var x = (this.Size.Width / 2) - (this.vaultProgressBar.Width / 2);
				var y = (this.Size.Height / 2);
				var loc = new Point(x, y);
				pb.Location = loc;
				pb.Visible = true;

				// IProgress
				var progress = new Progress<ProgressBarMessage>((mess) =>
				{
					this.backgroundWorkerGit.ReportProgress(mess.Percent, mess);
				});

				this.backgroundWorkerGit.RunWorkerAsync(progress);
			}
			return;
		}
	}

	bool _DoCloseStuffCompleted = false;

	private void backgroundWorkerGit_DoWork(object sender, DoWorkEventArgs e)
	{
		var arg = e.Argument as Progress<ProgressBarMessage>;
		this.GameFileService.GitAddCommitTagAndPush(arg);
	}

	private void backgroundWorkerGit_ProgressChanged(object sender, ProgressChangedEventArgs e)
	{
		var pb = this.vaultProgressBar;
		var mess = e.UserState as ProgressBarMessage;
		pb.Title = mess.Title;
		pb.Value = e.ProgressPercentage;
		pb.Invalidate();
	}

	private void backgroundWorkerGit_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
	{
		var pb = this.vaultProgressBar;
		pb.Visible = false;
		pb.SendToBack();

		_DoCloseStuffCompleted = true;
		this.Close();// Close again but it will pass
	}
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Waiting BackgoundWorker.DoWork() to finish
DoEvents() is a nasty hack to allow the RunWorkerCompleted event handler to run. Never write code like this, re-entrancy bugs kill you dead....
Read more >
Cancelling the BackgroundWorker
The cancel button simply calls the CancelAsync() method - this will signal to the worker that the user would like to cancel the...
Read more >
Thread: Modal Wait Dialogue with BackgroundWorker
You basically write a normal DoWork event handler for a BackgroundWorker, as well as optional ProgresssChanged and RunWorkerCompleted event ...
Read more >
C# BackgroundWorker and ProgressBar with Cancellation
Yet background worker provides us with a simple class called background worker that abstracts away the difficulty leaving it fairly simple.
Read more >
using a background worker but wait for completion.
Put() method until the background work has finished? I cannot see a "RunWorkerSynch" method, to run it synchronously, so am I using the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found