Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace CurrencyComprasionClient
  14. {
  15. public partial class Form1 : Form
  16. {
  17. const int SERV1_PORT = 2048;
  18. const int SERV2_PORT = 2049;
  19. const int SERV3_PORT = 2050;
  20. const string IP = "localhost";
  21. string message = "";
  22.  
  23. public Form1()
  24. {
  25. InitializeComponent();
  26. }
  27.  
  28. private async void button1_Click(object sender, EventArgs e)
  29. {
  30. int threadID;
  31. var currencyList = new List<string>();
  32. threadID = (int)AppDomain.GetCurrentThreadId();
  33. Console.WriteLine("1. " + threadID);
  34. foreach(string currency in checkedListBox1.CheckedItems)
  35. {
  36. currencyList.Add(currency);
  37. }
  38. this.message = string.Join(" ", currencyList.ToArray());
  39. try
  40. {
  41. threadID = (int)AppDomain.GetCurrentThreadId();
  42. Console.WriteLine("2. " + threadID);
  43. var task = ClientTask(SERV1_PORT);
  44. threadID = (int)AppDomain.GetCurrentThreadId();
  45. Console.WriteLine("2.a " + threadID);
  46. var task2 = ClientTask(SERV2_PORT);
  47. threadID = (int)AppDomain.GetCurrentThreadId();
  48. Console.WriteLine("2.a " + threadID);
  49. var task3 = ClientTask(SERV3_PORT);
  50. threadID = (int)AppDomain.GetCurrentThreadId();
  51. Console.WriteLine("2.a " + threadID);
  52. await Task.WhenAll(task, task2, task3);
  53.  
  54. threadID = (int)AppDomain.GetCurrentThreadId();
  55. Console.WriteLine("3. " + threadID);
  56. }
  57. catch (Exception exception)
  58. {
  59. MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  60. }
  61. threadID = (int)AppDomain.GetCurrentThreadId();
  62. Console.WriteLine("4. " + threadID);
  63. }
  64.  
  65. async Task ClientTask(int port)
  66. {
  67. int threadID = (int)AppDomain.GetCurrentThreadId();
  68. Console.WriteLine("5. " + threadID);
  69.  
  70. TcpClient client = new TcpClient();
  71. client.Connect(IP, port);
  72. var msgBuffer = Encoding.ASCII.GetBytes(this.message);
  73. var serverAnswer = new byte[1024];
  74. threadID = (int)AppDomain.GetCurrentThreadId();
  75. Console.WriteLine("5a. " + threadID);
  76. await client.GetStream().WriteAsync(msgBuffer, 0, this.message.Length).ContinueWith(
  77. async (tsk) =>
  78. {
  79. threadID = (int)AppDomain.GetCurrentThreadId();
  80. Console.WriteLine("6. " + threadID);
  81.  
  82. int lnt = await client.GetStream().ReadAsync(serverAnswer, 0, 1024);
  83. var serverMessage = Encoding.Default.GetString(serverAnswer).Substring(0, lnt);
  84. var package = serverMessage.Split(' ').ToList();
  85. threadID = (int)AppDomain.GetCurrentThreadId();
  86. Console.WriteLine("6a. " + threadID);
  87. SetTextLabels(package, port);
  88.  
  89. threadID = (int)AppDomain.GetCurrentThreadId();
  90. Console.WriteLine("7. " + threadID);
  91. });
  92.  
  93. threadID = (int)AppDomain.GetCurrentThreadId();
  94. Console.WriteLine("8. " + threadID);
  95. }
  96.  
  97. private void SetTextLabels(List<string> package, int port)
  98. {
  99. int threadID = (int)AppDomain.GetCurrentThreadId();
  100. Console.WriteLine("9. " + threadID);
  101.  
  102. if (package.Count == 0)
  103. {
  104. MessageBox.Show("Couldn't load rates correctly. Try again.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  105. return;
  106. }
  107. Label euroLabel, usdLabel, chfLabel;
  108. switch(port)
  109. {
  110. case SERV1_PORT:
  111. euroLabel = firstEuro;
  112. usdLabel = firstUsd;
  113. chfLabel = firstChf;
  114. break;
  115. case SERV2_PORT:
  116. euroLabel = secondEuro;
  117. usdLabel = secondUsd;
  118. chfLabel = secondChf;
  119. break;
  120. default:
  121. euroLabel = thirdEuro;
  122. usdLabel = thirdUsd;
  123. chfLabel = thirdChf;
  124. break;
  125. }
  126. if (package.Contains("EUR"))
  127. {
  128. int index = package.IndexOf("EUR");
  129. euroLabel.Invoke(new Action(() => euroLabel.Text = package[index + 1]));
  130. }
  131. if (package.Contains("USD"))
  132. {
  133. int index = package.IndexOf("USD");
  134. usdLabel.Invoke(new Action(() => usdLabel.Text = package[index + 1]));
  135. }
  136. if (package.Contains("CHF"))
  137. {
  138. int index = package.IndexOf("CHF");
  139. chfLabel.Invoke(new Action(() => chfLabel.Text = package[index + 1]));
  140. }
  141. }
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement