-1

Edit: As per suggestions in the comments, I will list out the actual functions of both A and B and what I am trying to achieve.

I am trying to create a loading animation in a C# Console Application. Function A repeatedly (hence why while true) draws characters on the screen (the animation), whilst Function B executes some code in the background.

As such, I need function A to stop once function B is completed, since that is when the program is "loaded".

I have two functions.

public static void A(){
    while(true){//repeatedly do one thing}
}

public static void B(){
        //do something
        //do something
        //do something
    }
static void Main(string[] args) {//main func}

Basically, I want to execute A (since it is a while loop) as long as B is running. Once B has finished executing its code, A will stop executing.

I have tried Parallel.Invoke but the problem with that is Parallel.Invoke waits for will wait for both functions to finish executing before it stops. Since function A is a never ending loop, Parallel.Invoke will never end as well.

Any suggestions is thanked in advance.

Leo Feng
  • 59
  • 8
  • You could change the `while(true)` to `while(flag)`, when B is finished, let it change the flag to false. Or, give the thread running A to B, when B exits, let B terminate the thread. – Brian Holsen Jun 08 '20 at 02:15
  • 2
    Please tell us what `A` and `B` **actually do**. As is, this feels like a XY Problem (https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – mjwills Jun 08 '20 at 02:21
  • @mjwills So essentially I'm trying to create a sort of loading animation in a C# Console App. Function B is executing code in the background and Function A is drawing the animation on the actual Console. – Leo Feng Jun 08 '20 at 02:51
  • Parallel.Invoke ought to work if `A` says `while(!done)` and the last line of `B` says `done = true;` Obviously you'll have to choose a place to put a `bool done = false;` accessible to both. – Wyck Jun 08 '20 at 03:34

3 Answers3

0

Can async function solve your problem?

public static void A(){
    var task = Task.Run(new Action(B));
    while(!task.IsCompleted){
        //repeatedly do one thing
    }
}
public static void B(){
        //do something
        //do something
        //do something
}
static void Main(string[] args) {//main func}
0

The above responses are appreciated. However, I think I've found a even simpler method, at least for what I am trying to do. I am going to leave it here for future reference in case someone is trying to do the same.

I solved the problem by multithreading. I started function A as a child thread, then executed function B in the main thread. Once function B has finished, I terminated the child thread containing function A. Code looks something like this:

ThreadStart funcA = new ThreadStart(functionA);
Thread funcAThread = new Thread(funcA);
funcAThread.Start();
functionB();
funcAThread.Stop();

This might not work for every case but if you just want to print some loading animation whilst your code executes in the background, this is (from what I can tell), the simplest way with the least lines of code.

Leo Feng
  • 59
  • 8
-1

Something like this might work

using System;
using System.Threading;
using System.Threading.Tasks;

namespace Test {
  class Program {
    static void Main (string[] args) {
        Task t1 = Task.Factory.StartNew (MethodB);
        Task t2 = Task.Factory.StartNew (() => MethodA (t1));
        t2.Wait ();
    }

    static void MethodA (Task dependentTask_) {
        while (true) {
            if (dependentTask_.Status != TaskStatus.Running) {
                Console.WriteLine ("Method B completed. Exiting Method A");
                break;
            }
            Console.WriteLine ("Waiting for Method B to complete");
            Thread.Sleep (200);
        }
    }

    static void MethodB () {
        Console.WriteLine ("Method B running");
        Thread.Sleep (1000);
        Console.WriteLine ("Method B still running");
        Thread.Sleep (1000);
        Console.WriteLine ("Method B still running");
        Thread.Sleep (1000);
        Console.WriteLine ("Method B completed");
    }
}
Vishwa
  • 195
  • 1
  • 5