changeset 21:d488eb23a29f

add ThreadTest
author riono <e165729@ie.u-ryukyu.ac.jp>
date Thu, 03 Dec 2020 19:04:33 +0900
parents 3aaa77e12493
children 970c7f587126
files Christie_net.csproj Test/RewritingTest/CreateThread.cs Test/RewritingTest/SocketLisnerThread.cs daemon/AcceptThread.cs
diffstat 4 files changed, 99 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Christie_net.csproj	Tue Dec 01 20:23:09 2020 +0900
+++ b/Christie_net.csproj	Thu Dec 03 19:04:33 2020 +0900
@@ -3,7 +3,7 @@
     <PropertyGroup>
         <OutputType>Exe</OutputType>
         <TargetFramework>netcoreapp3.1</TargetFramework>
-        <StartupObject>EnumInit</StartupObject>
+        <StartupObject>SocketLisnerThread</StartupObject>
     </PropertyGroup>
 
     <ItemGroup>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Test/RewritingTest/CreateThread.cs	Thu Dec 03 19:04:33 2020 +0900
@@ -0,0 +1,41 @@
+using System;
+using System.Threading;
+
+public class CreateThread {
+    public static void Main () {
+        Console.WriteLine ("スタート");
+
+        // DoSomethingメソッドを別のスレッドで実行するThreadオブジェクトを作成する
+        //Thread t = new Thread (new ThreadStart (DoSomething));
+        // スレッドを開始する
+        //t.Start ();
+
+        //t.IsBackground = true;
+
+        //t.Join ();
+
+        CreateThread createThread = new CreateThread();
+        createThread.Run();
+        
+        Console.WriteLine ("Enterキーを押してください");
+        Console.ReadLine ();
+    }
+
+    public void Run() {
+        Thread thread = new Thread(DoSomething);
+        thread.Start();
+        thread.Join();
+    }
+
+    // 別スレッドで実行するメソッド
+    private static void DoSomething () {
+        // 長い時間のかかる処理があるものとする
+        for (long i = 0; i < 1000000; i++) {
+            Console.Write("1");
+        }
+
+        // 処理が終わったことを知らせる
+        Console.WriteLine ("終わりました");
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Test/RewritingTest/SocketLisnerThread.cs	Thu Dec 03 19:04:33 2020 +0900
@@ -0,0 +1,53 @@
+using System;
+using System.Net;
+using System.Net.Sockets;
+using System.Text;
+using System.Threading;
+
+public class SocketLisnerThread {
+    Socket socket;
+
+    public SocketLisnerThread (Socket socket) {
+        this.socket = socket;
+    }
+
+    public void Run () {
+        Thread thread = new Thread (new ThreadStart (MethodThread));
+        thread.Start();
+        
+    }
+
+    private void MethodThread() {
+        Socket listener = socket.Accept ();
+        while (true) {
+            Console.WriteLine ("Accept:" + listener.LocalEndPoint);
+            Thread.Sleep(1000);
+        }
+    }
+
+    public static void Main () {
+        IPHostEntry host = Dns.GetHostEntry ("localhost");
+        IPAddress ipAddress = host.AddressList[0];
+        IPEndPoint localEndPoint = new IPEndPoint (ipAddress, 11000);
+
+        try {
+            Socket ss = new Socket (ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
+            ss.Bind(localEndPoint);
+            ss.Listen(10);
+
+            // while (true) {
+            //     Console.WriteLine("Accept:" + listener.LocalEndPoint);
+            // }
+         
+            SocketLisnerThread newThread = new SocketLisnerThread (ss);
+            newThread.Run ();
+            
+            // Console.WriteLine("fin");
+            // listener.Shutdown(SocketShutdown.Both);
+            // listener.Close();
+        } catch (Exception e) {
+            Console.WriteLine (e.ToString ());
+        }
+
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/daemon/AcceptThread.cs	Thu Dec 03 19:04:33 2020 +0900
@@ -0,0 +1,4 @@
+namespace Christie_net.daemon {
+public class AcceptThread {
+}
+}
\ No newline at end of file