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

  • Home
  • SEARCH
  • 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 9037283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:12:40+00:00 2026-06-16T09:12:40+00:00

I have a winforms app with a long running task and two buttons. One

  • 0

I have a winforms app with a long running task and two buttons.
One button Start and one Stop.

A new task with a cancellation Token starts when I press the start button.
And if I press the Stop button the cancellation Token’s Cancel Method gets called.

I want the UI to be usable during all the time so where do I put the try, catch block for this job. In all the examples I saw they put it around t.Wait();

But if I do that the UI freezes and that is the reason why I used a Task in the first place, to continue using the ui while doing the task. So where to put the try catch block without using Task.Wait.

Start button:

 tokenSource2 = new CancellationTokenSource();
   ct = tokenSource2.Token;
   t = new Task(doStart, ct);
   t.Start();

Stop button:

tokenSource2.Cancel();
  • 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-16T09:12:42+00:00Added an answer on June 16, 2026 at 9:12 am

    You could update doStart to handle the cancellation event and exit the task gracefully so you wouldn’t need to use Wait at all e.g.

    public void doStart(CancellationToken token)
    {
        while(...)
        {
            ...
            if (token.IsCancellationRequested)
                break;
        }
    }
    

    Alternatively, you could wait for the task result on another thread e.g.

    Thread.QueueUserWorkItem((state) =>
    {
        try
        {
            t.Wait();
        }
        catch(...)
        {
            ...
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WinForms app with one consumer and one producer task. My producer
I have a winforms app that initially displays a window with two file dialog
I have a winforms app and the main (and only) form has several buttons.
I have a winforms app. I give it to three clients and each one
I have a Windows service which performs a fairly long running task. At the
I have a winforms app. Inside, I have one panel (panel1), and inside this
I have a winforms app with a main form (fMain) and two child forms
I have a WinForms .NET app that, according to the Vista Task Manager with
So we have a winforms app that in the task manager should anywhere from
I have WinForms app and multiple forms and want to use ErrorProvider component on

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.