Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check stdin in C# while doing something

I have a program like this:

Class a
{
    main()
    {
        while(abc)
        {
            f(a);
            g(b);
        }
    }
}

but I need to check stdin for some important command. The programm should work will I receive a command from stdin. and after the command I should decide to continue execution or doing something else. What should I do?

like image 708
Masoud Avatar asked Nov 29 '25 16:11

Masoud


1 Answers

Execute f and g in different threads with some sort of variable accessed by both to propagate the state change.

Class Program
{
  public static void Main()
  {
    ThreadPool.QueueUserWorkItem(() => f(a));
    ThreadPool.QueueUserWorkItem(() => g(b));

    while(true)
    {
      if(Console.Readline() == "something")
        //do something
    }
  }

  private static void f (param)
  {
    while(abc)
      //do work
  }
}
like image 97
Femaref Avatar answered Dec 01 '25 05:12

Femaref



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!