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 3940048
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:22:18+00:00 2026-05-20T00:22:18+00:00

I have a XAML form that I would like two independent features threaded out

  • 0

I have a XAML form that I would like two independent features threaded out of the main XAML execution thread: 1.) a timer control that ticks up, and 2.) implement a pause button so I can freeze a set of procedural set of activities and clicking on the pause button again will resume.

When I start the form, the entire app freezes. Because of this I can’t interact with btnPause control. I got the timer class to work, but it (lblTimer) updates in chunks, or whenver it looks like the CPU isn’t busy on the main thread.

I tried to create a class “Task” that wraps a class or method in its own thread so I can control.

    Automation.Task AutomationThread = new Automation.Task();
    Automation.Task TimerThread = new Automation.Task();

Later on, I attempted to create a stopwatch class and assign the TimerThread to it so I can manage it independently.

    Automation.WatchTimer stopwatch = new Automation.WatchTimer(lblTimer, TimerThread);


class Task
{
    private ManualResetEvent _shutdownFlag = new ManualResetEvent(false);
    private ManualResetEvent _pauseFlag = new ManualResetEvent(true);

    Thread _thread;

    public Task() { }

    public void Start()
    {
        _thread = new Thread(DoWork);
        _thread.Start();
    }

    public void Resume()
    {
        _pauseFlag.Set();
    }

    public void Stop()
    {
        _shutdownFlag.Set();
        _pauseFlag.Set();
        _thread.Join();
    }

    public void DoWork()
    {
        do
        {
            _pauseFlag.WaitOne(Timeout.Infinite);
        }
        while (!_shutdownFlag.WaitOne(0));
    }

    // I AM NOT SURE IF THIS IS THE RIGHT APPROACH
    public void DoWork(Func<void> method????)
    {
        do
        {
            // NOT SURE WHAT TO PUT HERE
            // This is where I want it to do a method 
            // that wraps a long chain of procedural items
            // where I can pause (block) and unpause through the UI
        }
        while (!_shutdownFlag.WaitOne(0));
    }

class WatchTimer
{
    public WatchTimer(System.Windows.Controls.Label lbl, Automation.Task worker)
    {
        lblField = lbl;
        worker.Start();
    }

    private System.Windows.Controls.Label lblField;

    Timer time = new Timer();
    Stopwatch sw = new Stopwatch();

    public void Start()
    {
        time.Start();
        sw.Start();
        time.Tick += new EventHandler(time_Tick);
    }

    public void Stop()
    {
        time.Stop();
        sw.Stop();
    }

    public void Reset()
    {
        sw.Reset();
        lblField.Content = this.elapsedTime();
    }

    public string elapsedTime()
    {
        TimeSpan ts = sw.Elapsed;
        // return formatted TimeSpan value
        return String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);
    }

    private void time_Tick(object sender, EventArgs e)
    {
        lblField.Content = this.elapsedTime();
    }

}

The timer appears to work, but it still spits updates in chunks – does this mean it’s not on its own thread?

  • 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-05-20T00:22:19+00:00Added an answer on May 20, 2026 at 12:22 am

    The apparent hang is caused because one CPU is pegged at 100%. This is because the initial state of the _pauseFlag is set to signaled. So when the main thread calls new Thread(DoWork), the DoWork is constantly blowing past the _pauseFlag.WaitOne and running in a tight do/while loop.

    To wrap some method in a thread, with pausing, this could be doable. You would have to remove the do/while from the DoWork method. So your sequential code in DoWork would have to frequently check the paused event signaled state, in between the subtasks, inner loops, or whatever you would have in the sequential work method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a ComboBox control on a form which will be
I have a WPF control that I would like to overlay onto a WinForms
I have a flowdocument in WPF that looks like this: <FlowDocument xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml> <Paragraph
I have a listbox on my xaml form that I bound to a List(Of
Suppose I have some XAML like this: <Window.Resources> <v:MyClass x:Key=whatever Text=foo\nbar /> </Window.Resources> Obviously
I have a simple WPF (XAML) file that has some animated shapes and text.
I have a ListView in a Windows Form that I bind a list of
I have a form that is generated based on several DataTemplate elements. One of
I'm trying to integrate a WinForms Form inside a WPF XAML page. I have
I have multiple xaml based pages stored as children of a canvas on another

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.