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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:22:47+00:00 2026-06-14T21:22:47+00:00

Simple as this! This is my attempt at one, which requires that functions to

  • 0

Simple as this! This is my attempt at one, which requires that functions to be threaded with it use a Pause() function through itself in pausable sections.

using System;
using System.Threading;

class BlackThread {
    private bool paused;
    private Thread innerThr;

    // ---

    public bool IsAlive {
        get {
            return innerThr.IsAlive;
        }
    }

    // ===

    public void SetAndGo (ThreadStart start) {
        paused = false;

        innerThr = new Thread(start);
        innerThr.Start();
        WaitForIt();
    }

    // ---

    public void Pause() {
        paused = true;
        while (paused);
    }

    public void Unpause() {
        paused = false;
    }

    public void WaitForIt() {
        while(!paused && IsAlive);
    }

    public void Continue() {
        Unpause();
        WaitForIt();
    }
}

class MainClass {
    static void pausableFunction (BlackThread self) {
        Console.WriteLine("* Waiting...");
        self.Pause();
        Console.WriteLine("* Doing stuff.");
        self.Pause();
        Console.WriteLine("* Finished!");
    }

    static void Main() {
        BlackThread noir = new BlackThread();
        noir.SetAndGo(() => pausableFunction(noir));

        while (noir.IsAlive) {
            Console.Write("> ");
            Console.ReadKey();
            noir.Continue();
        }
    }
}

Sadly, it’s not one that can be paused at any time, but a thread for functions that require to wait for outside processing to be able to continue. Like an action by a game mob that requires its frame to be drawn by the draw loop before it can continue, and the mob’s A.I.’s is processed in the game’s main loop.

I guess it’d make it some kind of pseudo-thread? Anyway.

It would allow the mob to process this action bit by bit every loop, instead of cascading checkings in its A.I. like…

if mob is doing action {
    if mob has already done this previous part of the action {
        do the following part
    }
}

…it’d rather be like this, in a thread:

do the first step of the action
Wait for it to be rendered...
do the following step of the action
Wait for it to be rendered...
do the last step of the action
(Action ends here, no need to wait for anything anymore)

Now, my implementation has a bug which I cannot figure out how to fix. When it’s supposed to unpause the BlackThread, it remains paused in the function (in this case, pausableFunction()) that uses it. I guess it’s because of how the instance is passed?

If it’s what I’m guessing – that is, something (and I’d guess it’s bool paused) is passed by value instead of reference – how could I fix it?

I’m really used to the pointers of C and C++, so sometimes I get a bit tangled when dealing in C# with the communication of an object’s values between scopes.


This here is a version of the code the works, a prototype to say:

using System;
using System.Threading;

class Program {
    static bool paused;

    static void Pause() {
        paused = true;
        while (paused);
    }

    static void Unpause() {
        paused = false;
    }

    static void WaitForIt(Thread waited) {
        while(!paused && waited.IsAlive);
    }

    static void Continue (Thread ToStop) {
        Unpause();
        WaitForIt(ToStop);
    }

    static void SetAndGo (out Thread thread, ThreadStart Start) {
        thread = new Thread(Start);
        thread.Start();
        WaitForIt(thread);
    }

    // ---

    static void thr (string chant) {
        // Console.WriteLine("Waiting...");
        // Pause();
        // Console.WriteLine("{0}", chant);
        // Pause();
        // Console.WriteLine("Well, I'm finished!");

        Console.WriteLine("I'm finished!");
    }

    static void Main() {
        // Thread tt = new Thread(() => thr());
        // tt.Start();
        // WaitForIt(tt);

        Thread tt;
        SetAndGo(out tt, (() => thr("I'm doing stuff.")));

        while (tt.IsAlive) {
            Console.Write("> ");
            Console.ReadKey();
            Continue(tt);
        }
    }
}

I’m only not using it because I’d rather have everything in charge of a specific class for the matter, something that would also enhance readability.

  • 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-14T21:22:48+00:00Added an answer on June 14, 2026 at 9:22 pm

    Alright, I’ve accomplished what I was trying already, so I’ll leave my code here for future reference!

    This is the BlackThread class in the end:

    using System;
    using System.Threading;
    
    class BlackThread { 
        //* ===== *//
    
        private AutoResetEvent pauser = new AutoResetEvent(false);
        private AutoResetEvent waiter = new AutoResetEvent(false);
    
        private Thread innerThr;
    
        // ----- //
    
        public bool IsActing {
            get {
                if (innerThr != null) return innerThr.IsAlive;
                else return false;
            }
        }
    
        //* ===== *//
    
        public void KickStart_(ThreadStart start) {
            innerThr = new Thread(start);
            innerThr.Start();
    
            WaitForIt();
        }
    
        // ----- //
    
        // FOR THE THREADED FUNCTION
        public void Wait() {
            waiter.Set();
            pauser.WaitOne();
        }
    
        public void End() {
            waiter.Set();
        }
    
        // ----- //
    
        // FOR BLACKTHREAD MANAGING
        private void WaitForIt() {
            waiter.WaitOne();
        }
    
        public void Continue() {
            if (IsActing) {
                pauser.Set();
                WaitForIt();
            }
        }
    
        //* ===== *//
    }
    

    And here, an example of its use:

    class MainClass {
        static void pausableFunction() {
            Console.WriteLine("* Waiting...");
    
            Event.Wait();
    
            Console.WriteLine("* Doing stuff.");
            Thread.Sleep(1000);
    
            Event.Wait();
    
            Console.WriteLine("* Finished!");
    
            Event.End();
        }
    
        static void anotherFunction(int foo) {
            Console.WriteLine("* Wanna know the value of a number?");
    
            Event.Wait();
    
            Console.WriteLine("* I'll tell you. It's {0}!", foo);
    
            Event.End();
        }
    
        static void simpleFunction() {
            Console.WriteLine("* I'm done already!");
        }
    
        static BlackThread Event = new BlackThread();   
        static Random Rand = new Random();
    
        static void Main() {        
            int r;
    
            do {
                if (!Event.IsActing) {
                    Console.WriteLine();
                    r = Rand.Next(3);
    
                    if (r == 0) {
                        Event.KickStart_(() => pausableFunction());
                    }
                    else if (r == 1) {
                        simpleFunction();
                    }
                    else {
                        Event.KickStart_(() => anotherFunction(Rand.Next(20) + 1));
                    }
                }
                else {
                    Event.Continue();
                }
    
                Console.Write("> ");
                Console.ReadKey();
            } while(true);
        }
    }
    

    What I’ve opted to use in the end were two AutoResetEvent handlers. One is managed in the function of the thread that requires pausing, and that pauses the main loop, the waiter ARE, and another, the pauser ARE, managed in the main loop, and that pauses the thread with the function with support for BlackThread; that is, has acess to a BlackThread instance.

    In this case I’ve used a static BlackThread object, but it can also be passed as a parameter to the function.

    And yes, it’s named after the Buddhist hell!

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

Sidebar

Related Questions

This is my first attempt at responsive design, so I'm keeping it simple. I
I'm trying to create a simple threading procedure (granted this is my first attempt
I'm not sure how simple this would be, but I'm using a script which
consider this simple function def foo(l=[]): if not l: print List is empty else
This is my first attempt to use WCF so there may be something fundamentally
I have a php function that brings in a simple arraycollection (called QuestionSeries) to
I am running one simple hadoop application which collect information from a 64MB file,
this is my first question on here. The question is simple - # this
This simple query session = com.jthink.songlayer.hibernate.HibernateUtil.getSession(); Query q = session.createQuery(recNo from SongChanges); giving this
This simple script should theoretically check the form for errors and then print any

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.