wątki rozpoczynają pracę dopiero po zakończeniu Main

0

@ŁF: Bardziej szczegółowo nie potrafię tego tematu zatytułować (poprzedni temat "Multithreading"), gdyż dotyczy od niepoprawnej implementacji wielowątkowości. Wzorowałem się na tym temacie: [C#] MultiThreading Problem , gdyż nie został on przez nikogo usunięty. Tagi z tego co zauwałem są już automatycznie dodawane.

Chciałbym, żeby w tym programie były wyświetlane kolejne liczby od 0 do 100. Ale trybie debug wątki rozpoczynają pracę dopiero po zakończeniu metody main. Bez debugowania pokazuje się tylko 1 i 0. Próbowałem robić to na różne sposoby, ale za każdym razem dostaje inne wyniki. Jak dodam jakąkolwiek funkcje (np Console.WriteLine()) do metody main po rozpoczęciu wątku to wątki dobrze działąją. Co może być przyczyną takiego nieprzewidywalnego zachowania? Próbowałem zakłądać blokady w różnych miejscach, ale bez skutku.

 
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.Remoting.Messaging;
 
namespace ConsoleApplication1
{
 
 
    class Program
    {
        static int counter = 0;
        static object o=new object();
 
 
        static void Main(string[] args)
        {
            Action a = new Action(WriteY);
            Action b = new Action(WriteX);
 
            IAsyncResult ar=a.BeginInvoke(null, null);
            IAsyncResult ar2= b.BeginInvoke(null, null);
            Console.WriteLine();
            Console.ReadKey();
        }
 
        static void WriteX()
        {
            while (counter < 100)
            {
                lock (o)
                {
                    Console.WriteLine(counter++);
                }
            }
        }
 
        static void WriteY()
        {
            while(counter<100)
            {
                lock (o)
                {
                    Console.WriteLine(counter++);
                }
            }
        }
 
    }
 
 
}
0

Polecam uważną literaturę http://msdn.microsoft.com/en-us/library/2e08f6yc%28v=vs.80%29.aspx

Np.

		static void Main(string[] args)
		{
			Action a = WriteY;
			Action b = WriteX;

			var ra = a.BeginInvoke(null, null);
			var rb = b.BeginInvoke(null, null);

			ra.AsyncWaitHandle.WaitOne();
			rb.AsyncWaitHandle.WaitOne();

			Console.WriteLine();
			Console.ReadKey();
		}
0

Po zmienieniu z ReadKey() na ReadLine() wszystko dobrze działa. Przypuszczam, że ReadKey() może w specyficzny sposób działać.

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.Remoting.Messaging;

namespace ConsoleApplication1
{


    class Program
    {
        static int counter = 0;
        static object o = new object();


        static void Main(string[] args)
        {
            Thread t = new Thread(new ParameterizedThreadStart(show));
            t.Start(5);
            
            //Console.ReadLine();
           
            Console.ReadKey();
        }

        static void show(object val)
        {
            if (val is int)
            {
                while (counter < (int)val)
                {
                    Console.WriteLine(counter++);
                }
            }
        }

    }


}
0

morał jest taki: klasa Console nie jest thread-safe.

1 użytkowników online, w tym zalogowanych: 0, gości: 1