Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8309145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:01:12+00:00 2026-06-08T19:01:12+00:00

I have a problem that I cannot solve even after looking at the various

  • 0

I have a problem that I cannot solve even after looking at the various information about Dispatcher.Invoke.

There is a MainWindow that creates a task, starts that task and then opens a progress window. When the task completes, I need to close the progress window. Here’s what I have:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Task task = new Task(_vm.WriteToDB);
        task.ContinueWith(ExceptionHandler, TaskContinuationOptions.OnlyOnFaulted);
        task.ContinueWith(RanToCompletion, TaskContinuationOptions.OnlyOnRanToCompletion);
        task.Start();

        _frmProgress = new Progress(task);
        _frmProgress.DataContext = _vm;
        _vm.ProgressText = "Connecting to update server...";
        _frmProgress.ShowDialog();
    }

    public void RanToCompletion(Task task)
    {
        Progress.FormCloseDelegate FormCloseDelegate = new Progress.FormCloseDelegate(_frmProgress.Close);

        if (_frmProgress.Dispatcher.CheckAccess())
            _frmProgress.Close();
        else
            _frmProgress.Dispatcher.Invoke(DispatcherPriority.Normal, FormCloseDelegate);
    }

FormCloseDelegate is defined in the Progress window:

public delegate void FormCloseDelegate();

With the current implementation above, the code compiles and runs but the Progress form is not closed once the task completes. Any ideas?

I’d also be open to other ideas to solve this. Maybe I’ve gone down the wrong path altogether.

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:01:13+00:00Added an answer on June 8, 2026 at 7:01 pm

    Well as I mentioned in comments, I was blocking closing of the Progress window when the user clicked on the close button with e.Cancel=true in the Window_Closing event. This was blocking the call in the code to close the window also.

    I updated the RanToCompletion method after realising that I didn’t need a custom delegate at all:

    public void RanToCompletion(Task task)
        {
            if (_frmProgress.Dispatcher.CheckAccess())
                _frmProgress.Close();
            else
                _frmProgress.Dispatcher.Invoke(DispatcherPriority.Normal, new ThreadStart(_frmProgress.Close));
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple problem that i cannot solve. I have a dictionary: aa
I am new to python and I have a serious problem that I cannot
I have a little newbie problem that I cannot figure out, I am having
I have a very simple C# DataTable problem that I cannot seem to wrap
I have this little problem, that I cannot figure out which arguments to pass
It is known that the halting problem cannot have a definite solution, one that
The basic problem I'm trying to solve is that I have a mix of
I have an interesting problem to solve here that may require some creative direction.
I simply cannot wrap my head around how to solve this problem and after
We have a problem in our team which we cannot solve :/ We made

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.