-6

I know this question had been answered a thousand times, but i still can't get the functions to work at the same time. Here is my code:

static void Main(string[] args)
{
    a();
    a();
}

static void a()
{
    string sampleString = "a";
    Console.WriteLine(sampleString);

    for (int i = 0; i < 10; i++)
    {
        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(0.1));
        Console.SetCursorPosition(0, Console.CursorTop -1);
        sampleString = " " + sampleString;
        Console.WriteLine(sampleString);
    }
}

The function just writes the letter 'a' and after 0.1 seconds adds a space behind it, not complicated. After the function ends, it does the same thing again. I want both of the functions to work at the same time, the first function in the first line and the second in the second line. Thanks in advance.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Denisk1ng
  • 33
  • 1
  • 6
  • 3
    Asynchronous programming is a really broad topic. I'd recommend reading [Asynchronous Programming Patterns](https://msdn.microsoft.com/en-us/library/jj152938(v=vs.110).aspx). – p.s.w.g Apr 28 '16 at 13:42
  • 3
    If the question has been answered a thousand of times, why you did not implemented any of the solutions instead of ask it again? – Gusman Apr 28 '16 at 13:43
  • He is asking for a way to come back to previously outputted line and add space to it. – Riad Baghbanli Apr 28 '16 at 13:44
  • 3
    Simultaneously means nothing. Even using asynchronous programming (that´s what you´re after) your computer is only able to process one task at a time. However you can switch between parallell tasks quite fast making you feel it happens simultaneously. – MakePeaceGreatAgain Apr 28 '16 at 13:45
  • @HimBromBeere Just a pedantic correction *Even using asynchronous programming (that´s what you´re after) your computer is only able to process one task at a time*, that's not true on multi-core processors... – Gusman Apr 28 '16 at 13:46
  • @HimBromBeere - Although multiple-core CPUs do run simultaneously. – Enigmativity Apr 28 '16 at 13:46
  • 1
    Something like `Parallel.Invoke(() => a(); () => a());`? `Parallel.For(0, 10, i => whatever);`? – Dmitry Bychenko Apr 28 '16 at 13:49
  • Even this would work: `Action action = a; action.BeginInvoke(null, null); action.BeginInvoke(null, null);` – Enigmativity Apr 28 '16 at 13:52

2 Answers2

2

You can use Parallel class:

static void Main(string[] args)
{
    Parallel.Invoke(a,a);
}

You functions will be run simultaneously.

Roman
  • 11,966
  • 10
  • 38
  • 47
0

An easy solution would be to create two threads, start them and wait for them to finish. For your specific question:

static void Main(string[] args)
{
    var t1 = new Thread(a); t1.Start();
    var t2 = new Thread(a); t2.Start();
    t1.Join(); t2.Join();
}

But of course, this does not solve your initial problem, writing into different lines of the console, as the a function does not know, which line to write on. You could solve this, by adding a parameter with the line number.

Furthermore, you'll have to protect your console output by a lock, because it's a single target where multiple sources write data into, which may lead to inconsistencies if not synchronized correctly. So I really suggest, that you read some tutorials on multi-threading.

derpirscher
  • 14,418
  • 3
  • 18
  • 35