2

Background

I am currently working on the architecture for a future software design. The design is one long running task (hours-long) with thousands of sequential calls to various libraries. I currently have a method of pausing this long-running task, that I would first like to discuss before asking my question.

The flow of control for the pause will work as follows:

  1. The user/operator clicks on a pause button.
  2. A ManualResetEvent is Set.
  3. The running task (described in first paragraph) will observe the ManualResetEvent and stops in its tracks.

The task might be some sequential looking thing such as...

/// <summary>
/// Some task that calls three wrapped library
/// methods.
/// </summary>
public void MyTaskMethod()
{
    // perform some library methods...
    WrappedLibraryMethodOne();
    WrappedLibraryMethodTwo();
    WrappedLibraryMethodThree();
}

The pausing methodology involves the following, wrapping the library methods in order to observe a ManualResetEvent as follows:

/// <summary>
/// Wraps a library method call, and also checks
/// to see if we're paused.
/// </summary>
public void WrappedLibraryMethodOne()
{
    // 1. Check if we should pause here.
    this.pause.WaitOne();

    // 2. Call the library method.
    this.myLibrary.LibraryMethod();
}

Note, that the ManualResetEvents do not adhere to any timeout. Additionally, all of the WrappedLibraryMethods look like this (hence the DRY-thing).

My problem with using this method, is that now I have to wrap all of my library methods. This almost becomes a DRY problem, where I am wrapping every single method that comes along.

Question

This way of doing things does work, and I don't have a huge issue with the fact that I am only creating an illusion of concurrency. However, should I try and stream-line this somehow?

  • I think we need a bit more information. Why do you need to wait or is it 'hit any key to continue'-pausing, for how long do you need to wait, is there any locking mechanism involved, does WaitOne() wait a set time or until an event has occured (has someone hit the any key) etc. You mention concurrency is there a reason you don't use threads? – Bent Apr 12 '16 at 13:57
  • @Bent I do state that I use a task... So I'm not sure where you're going with that... I'll do my best to add some more information. – Snoop Apr 12 '16 at 13:58
  • @Bent There you go. – Snoop Apr 12 '16 at 14:04
1

If I understand this correctly you have a long list of methods that should be called sequentially.

But you would like to be able to pause the execution between each call to a method, but you do not now where beforehand.

An algorithm like this might do the trick

public void MyTaskMethod()
{
    List methodObjList = GetListOfMethodObjs();

    foreach(var item in methodObjList)
    {
        this.pause.WaitOne();

        item.method();
    }
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.