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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:33:21+00:00 2026-06-09T01:33:21+00:00

Suppose I have two threads. On one thread, I’m performing a sequence of jobs.

  • 0

Suppose I have two threads. On one thread, I’m performing a sequence of jobs. On the other thread I’m waiting for user input to control those jobs (i.e., skip to the next job). For example (pseudo-C#):

public class Worker {

    public List<Job> Jobs { get; set; }

    public Worker(Controller anotherThread) {
        anotherThread.SkipJobRequested += OnSkipJobRequested;
    }

    public DoWork() {
        foreach (Job job in Jobs) {
            // Do various work...
        }
    }

    // Event that fires on Controller thread
    public void OnSkipJobRequsted(Object sender, EventArgs args) {
        // Somehow skip to the next job
    }
}

I’m uncertain how I should handle skipping to the next job. One possibility that occurs to me would be to have an instance variable (like, IsSkipRequested) that I set when a skip is requested, and check it at various junctures within DoWork().

Do any other patterns exist that I could use to handle this event?

  • 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-09T01:33:22+00:00Added an answer on June 9, 2026 at 1:33 am

    Another suggestion would be (and you have mentioned this above) if you made List<Jobs> a Queue<Jobs>, and rather than DoWork performing a foreach maintained the current executing job in your object, you could simply dequeue each item when ready for processing.

    The task parallel library allows you to specify a cancellation token (although you would probably have to pass this to your job execution code, and handle cancellation in there), which you can call RequestCancellation on when skip is pressed, and start the next job from the queue. Additionally, when firing off a new task you can specify an action to perform on completion, this would allow you to chain your tasks together into a sequential order and skip tasks when required. Below is an example without a cancellation token:

            _currentJob = Jobs.DeQueue();
            Task.Factory.StartNew(() => {_currentJob.execute();}, 
            () => 
            {
                //On task completion logic
                ExecuteNextJobFromQueue();                              
            }
    

    Be aware that this approach would probably work best if Job is performing multiple tasks and not one big blocking task as you would need to check for cancellation during job execution.

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

Sidebar

Related Questions

Suppose I have two tables Main and Other like the following: Main +----------+-----------------------------------+ |
Suppose we have a shared queue (implemented using an array), which two threads can
I have two threads that use an event for synchronization. In each thread they
Suppose we have many user processes running on Linux. Each process has many threads
Suppose I have two threads: A and B. When A creates an object(for example
Suppose I have this code executed by two different threads T1 and T2 that
Suppose I have two threads A and B that are both incrementing a ~global~
Suppose I have two threads(Thread1, Thread2) where the threads are accessing the cache for
You have two threads, a and b. Thread a is in a forever loop,
Suppose I have two tables, - emp(empId number(1),empName varchar2(50)) and - manager(manId number(5),managerName varchar2(100))

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.