###前提・実現したいこと
非同期処理を実装したのですが、これでは実行してる暇がなく、即終了してしまいます。
非同期処理に待機するにはどうすればいいでしょうか?
###該当のソースコード
C#
1using System; 2using System.IO; 3using System.Windows.Forms; 4using System.Drawing; 5using System.Drawing.Imaging; 6using System.Threading; 7using System.Threading.Tasks; 8using System.Net; 9using System.Net.Sockets; 10 11namespace Vishnu__Server_ 12{ 13 14 class Program 15 { 16 17 static int Port_Img = 2000; //画像ファイル用ポート 18 19 static TcpListener Lis_img = new TcpListener(IPAddress.IPv6Any, Port_Img); //画像ファイル用リスナー 20 static TcpClient Cli_img; //画像ファイル用クライアント 21 static NetworkStream Net_img; //画像ファイル用ネットワークスチーム 22 23 static void Main (string[] agrs) 24 { 25 26 //画面を録画する 27 Task Bec_Img = Task.Run(new Action(() => 28 { 29 30 //メモリースチーム 画像変換時に使用 31 MemoryStream Mem_Str = new MemoryStream(); 32 33 //現在のディスプレイの画像 34 Bitmap Bit_Dis = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); 35 36 //キャンバァス 37 Graphics Gra_Dis = Graphics.FromImage(Bit_Dis); 38 39 //IPv4でも接続できるようにする。 40 Lis_img.Server.SetSocketOption( 41 SocketOptionLevel.IPv6, 42 SocketOptionName.IPv6Only, 43 0); 44 45 //リスナー開始 46 Lis_img.Start(); 47 48 while (true) 49 { 50 51 //画像送信用メモリースチーム 52 MemoryStream Img_Str = new MemoryStream(); 53 54 //画像送信用バイト 55 byte[] bdat = new byte[1024]; 56 57 if(Lis_img.Pending()) 58 { 59 60 while (true) 61 { 62 63 //クライアント接続待ち 64 Cli_img = Lis_img.AcceptTcpClient(); 65 while (Cli_img.Connected) 66 { 67 68 //スクリーンショットを取る。 69 Gra_Dis.CopyFromScreen(new Point(0, 0), new Point(0, 0), Bit_Dis.Size); 70 71 //メモリースチームに画像をぶち込む 72 Bit_Dis.Save(Img_Str, ImageFormat.Png); 73 74 //バイトにメモリースチームの画像をぶち込む 75 bdat = Img_Str.ToArray(); 76 77 //ネットワークスチームの取得 78 Net_img = Cli_img.GetStream(); 79 80 //送信 81 Net_img.Write(bdat, 0, bdat.Length); 82 83 //3ミリ秒待つ 84 Thread.Sleep(3); 85 } 86 } 87 88 } 89 90 } 91 92 } 93 )); 94 95 } 96 97 98 } 99 100} 101
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/07/17 11:30