private void SendMessages() { portResultsFromSender = new Dictionary <int, int>(); BackgroundWorker bw = new BackgroundWorker { WorkerReportsProgress = true }; int numMessagesToSend = int.Parse(txtNumToSend.Text); bw.DoWork += new DoWorkEventHandler(delegate(object o, DoWorkEventArgs args) { BackgroundWorker b = o as BackgroundWorker; var tcpSender = new TcpSender { DestinationServer = txtEndpoint.Text.Trim(), DestinationPort = int.Parse(txtPort.Text.Trim()) }; for (int i = 1; i <= numMessagesToSend; i++) { var response = tcpSender.SendHL7(DefaultHL7Message); if (portResultsFromSender.ContainsKey(response.Port) == false) { portResultsFromSender.Add(response.Port, 1); } else { portResultsFromSender[response.Port] += 1; } b.ReportProgress((int)((double)i / numMessagesToSend * 100)); } }); // what to do when progress changed (update the progress bar for example) bw.ProgressChanged += new ProgressChangedEventHandler(delegate(object o, ProgressChangedEventArgs args) { lblProgress.Text = string.Format("{0}% Completed", args.ProgressPercentage); }); // what to do when worker completes its task (notify the user) bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object o, RunWorkerCompletedEventArgs args) { label1.Text = "Finished!"; portsResults.Visible = true; foreach (var item in portResultsFromSender.OrderByDescending(x => x.Value)) { portsResults.Items.Add(string.Format("Port {0}: {1} messages", item.Key, item.Value)); } }); bw.RunWorkerAsync(); }
public TcpSendResults SendMessages(SendHL7ViewModel model, TcpSender tcpSender) { int sendMsgCounter = 0; Stopwatch s = new Stopwatch(); try { s.Start(); for (sendMsgCounter = 0; sendMsgCounter < model.NumMessages; sendMsgCounter++) { tcpSender.SendHL7(model.HL7MessageToSend); } s.Stop(); return new TcpSendResults { TimeElapsed = s.Elapsed, NumberMsgsSent = sendMsgCounter }; } catch (Exception e) { return new TcpSendResults { TimeElapsed = s.Elapsed, NumberMsgsSent = sendMsgCounter, ExceptionDuringSend = e.Message }; } }
public TcpSendResults SendMessages(SendHL7ViewModel model, TcpSender tcpSender) { int sendMsgCounter = 0; Stopwatch s = new Stopwatch(); try { s.Start(); for (sendMsgCounter = 0; sendMsgCounter < model.NumMessages; sendMsgCounter++) { tcpSender.SendHL7(model.HL7MessageToSend); } s.Stop(); return(new TcpSendResults { TimeElapsed = s.Elapsed, NumberMsgsSent = sendMsgCounter }); } catch (Exception e) { return(new TcpSendResults { TimeElapsed = s.Elapsed, NumberMsgsSent = sendMsgCounter, ExceptionDuringSend = e.Message }); } }