Thread.Yield()的怪事。

先上代码:

执行代码,包含启动一个线程,在这个线程的最后 调用了

Thread.Yield();
然后,这个线程就一直运行着,没有结束掉。


可通过这个线程里创建的WCF服务是否有效判断。



class Program
    {
        static void Main(string[] args)
        {

            Thread td = new Thread(new ThreadStart(() =>
            {
                try
                {
                    ServiceFacade.Register();

                    Thread.Yield();
                }
                catch
                {
                    int t = 1;
                }
            }));
            td.Start();

            Console.WriteLine("Press <ENTER> to terminate");
            Console.ReadLine();
        }
    }



ServiceFacade.Register();
看 ServiceFacade.Register 代码:


public class ServiceFacade
    {
        public static void Register()
        {
            Uri baseAddress = new Uri("http://127.0.0.1:8001/demo/");

            ServiceHost Host = new ServiceHost(typeof(DemoService), baseAddress);

            Host.AddServiceEndpoint(typeof(IDemo), new BasicHttpBinding(), "http://127.0.0.1:8001/demo/");

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            Host.Description.Behaviors.Add(smb);

            Host.Open();
            Console.WriteLine("The manual WCF is running at " + baseAddress.ToString() + "WCF");

            //Host.Close();
        }
    }

DemoService代码:

[ServiceContract]
    public interface IDemo
    {
        [OperationContract]
        int GetAge(string name);

        [OperationContract]
        List<int> GetAges(IList<string> names);
    }


public class DemoService : IDemo
    {
        public int GetAge(string name)
        {
            return 10;
        }

        public List<int> GetAges(IList<string> names)
        {
            return new int[] { 1, 53, 23, 66 }.ToList();
        }
    }

以上是全部代码了,麻烦各位大大指点。


你可能感兴趣的:(Thread.yield)