При запуске метода с параметром делегатом в отдельном потоке виснет все приложение - C#

Узнай цену своей работы

Формулировка задачи:

Листинг программы
  1. public partial class MainWindow : Window
  2. {
  3. public delegate void ServerStart(Server.Send t);
  4. public MainWindow()
  5. {
  6. Server test = new Server();
  7. Thread t = new Thread(test.Start);
  8. t.Start();
  9. }
  10. public void WriteMessage(string msg)
  11. {
  12. ChatBox.Items.Add(msg);
  13. }
  14. private void Send_Click(object sender, RoutedEventArgs e)
  15. {
  16. Server test = new Server();
  17. ServerStart start_delegate = new ServerStart(test.Start);
  18. IAsyncResult result = start_delegate.BeginInvoke(WriteMessage, null, null);
  19. start_delegate.EndInvoke(result);
  20. InitializeComponent();
  21. }
  22. }
  23. namespace Chat
  24. {
  25. public class Server
  26. {
  27. public delegate void Send(string msg);
  28. public void Start(Send test)
  29. {
  30. TcpListener listen = null;
  31. try
  32. {
  33. int port = 4000;
  34. IPAddress iaddress = IPAddress.Parse("127.0.0.1");
  35. listen = new TcpListener(iaddress, port);
  36. listen.Start();
  37. byte[] bytes = new byte[256];
  38. string data = null;
  39. while (true)
  40. {
  41. TcpClient client = listen.AcceptTcpClient();
  42. NetworkStream stream = client.GetStream();
  43. int i;
  44. while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
  45. {
  46. data = Encoding.Unicode.GetString(bytes, 0, i);
  47. test(data);
  48. data = data.ToUpper();
  49. byte[] msg = Encoding.Unicode.GetBytes(data);
  50. stream.Write(msg, 0, msg.Length);
  51. }
  52. client.Close();
  53. }
  54. }
  55. catch
  56. {
  57. }
  58. finally
  59. {
  60. listen.Stop();
  61. }
  62. }
  63. public void Start()
  64. {
  65. TcpListener listen = null;
  66. try
  67. {
  68. int port = 4000;
  69. IPAddress iaddress = IPAddress.Parse("127.0.0.1");
  70. listen = new TcpListener(iaddress, port);
  71. listen.Start();
  72. byte[] bytes = new byte[256];
  73. string data = null;
  74. while (true)
  75. {
  76. TcpClient client = listen.AcceptTcpClient();
  77. NetworkStream stream = client.GetStream();
  78. int i;
  79. while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
  80. {
  81. data = Encoding.Unicode.GetString(bytes, 0, i);
  82. //test(data);
  83. data = data.ToUpper();
  84. byte[] msg = Encoding.Unicode.GetBytes(data);
  85. stream.Write(msg, 0, msg.Length);
  86. }
  87. client.Close();
  88. }
  89. }
  90. catch
  91. {
  92. }
  93. finally
  94. {
  95. listen.Stop();
  96. }
  97. }
  98. }
  99. }
не могу понять в чем проблема, буду рад помощи

Решение задачи: «При запуске метода с параметром делегатом в отдельном потоке виснет все приложение»

textual
Листинг программы
  1. Server test = new Server();
  2.             ServerStart start_delegate = new ServerStart(test.Start);
  3.             IAsyncResult result = start_delegate.BeginInvoke(WriteMessage, null, null);
  4.             start_delegate.EndInvoke(result);

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.5 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы