Теперь я вызываю метод службы в разных потоках. Я ожидал, что и метод ServiceCall, и метод ReadRooms будут запущены одинаково, но я получил неправильный результат ниже.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static void ReadRooms(int i)
{
Console.WriteLine("Reading Room::" + i);
Thread.Sleep(2000);
}
public static void CallService(int i)
{
Console.WriteLine("ServiceCall::" + i);
ReadRooms(i);
}
static void Main(string[] args)
{
Thread[] ts = new Thread[4];
for (int i = 0; i < 4; i++)
{
ts = new Thread(() =>
{
int temp = i;
CallService(temp);
});
ts.Start();
}
for (int i = 0; i < 4; i++)
{
ts.Join();
}
Console.WriteLine("done");
Console.Read();
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/184 ... in-c-sharp
Мобильная версия