private void ShowUpdateMessage(string message, params object[] args) { if (this.InvokeRequired) { this.Invoke((ThreadStart) delegate { StringBuilder outputText = new StringBuilder(); outputText.AppendFormat("[{0}] ", DateTime.Now.ToString()); outputText.AppendFormat(message, args); outputText.Append("\r\n"); MessagesOutput.AppendText(outputText.ToString()); Application.DoEvents(); }); } }
private void StartProcessing_Click(object sender, EventArgs e) { // Validate selection. MessagesOutput.Clear(); if (IDInput.CheckedIndices.Count == 0) { ShowUpdateMessage("No points selected for processing."); return; } if (!ValidateOutputFormat()) { return; } // Capture selection. DateTime startTime = DateTime.Parse(StartTimeInput.Text, CultureInfo.InvariantCulture.DateTimeFormat); DateTime endTime = DateTime.Parse(EndTimeInput.Text, CultureInfo.InvariantCulture.DateTimeFormat); try { StartProcessing.Enabled = false; this.Cursor = Cursors.WaitCursor; // Dispose previously created client. if (m_transmitClient != null) { m_transmitClient.Dispose(); } // Create new client. ShowUpdateMessage("Initializing client..."); List <object> state = new List <object>(); Dictionary <int, Metadata> metadata = new Dictionary <int, Metadata>(); state.Add(null); state.Add(startTime); state.Add(endTime); state.Add(RepeatDataProcessing.Checked); state.Add(OutputPlainTextDataFormat.Text); state.Add(int.Parse(ProcessDataAtIntervalSampleRate.Text)); state.Add(metadata); m_transmitStarts = 0; m_transmitCompletes = 0; m_transmitExceptions = 0; switch (OutputChannelTabs.SelectedIndex) { case 0: // TCP m_transmitClient = ClientBase.Create(string.Format("Protocol=TCP;Server={0}:{1}", TCPServerInput.Text, TCPPortInput.Text)); break; case 1: // UDP m_transmitClient = ClientBase.Create(string.Format("Protocol=UDP;Server={0}:{1};Port=-1", UDPServerInput.Text, UDPPortInput.Text)); break; case 2: // File m_transmitClient = ClientBase.Create(string.Format("Protocol=File;File={0}", FileNameInput.Text)); if (!AppendToExisting.Checked) { FileClient client = m_transmitClient as FileClient; if ((object)client != null) { client.FileOpenMode = FileMode.Create; } } break; case 3: // Serial m_transmitClient = ClientBase.Create(string.Format("Protocol=Serial;Port={0};BaudRate={1};Parity={2};StopBits={3};DataBits={4};DtrEnable={5};RtsEnable={6}", SerialPortInput.Text, SerialBaudRateInput.Text, SerialParityInput.Text, SerialStopBitsInput.Text, SerialDataBitsInput.Text, SerialDtrEnable.Checked, SerialRtsEnable.Checked)); break; } if ((object)m_transmitClient != null) { m_transmitClient.MaxConnectionAttempts = 10; m_transmitClient.SendDataStart += m_transmitClient_SendDataStart; m_transmitClient.SendDataComplete += m_transmitClient_SendDataComplete; m_transmitClient.SendDataException += m_transmitClient_SendDataException; ShowUpdateMessage("Client initialized."); // Connect the newly created client. ShowUpdateMessage("Connecting client..."); m_transmitClient.Connect(); if (m_transmitClient.CurrentState == ClientState.Connected) { // Client connected successfully. ShowUpdateMessage("Client connected."); // Queue all selected points for processing. StringBuilder selection = new StringBuilder(); Metadata definition; for (int i = 0; i < IDInput.CheckedItems.Count; i++) { if (selection.Length > 0) { selection.Append(','); } definition = (Metadata)IDInput.CheckedItems[i]; selection.Append(definition.PointID); metadata.Add(definition.PointID, definition); } state[0] = selection.ToString(); if (ProcessDataInParallel.Checked) { ThreadPool.QueueUserWorkItem(Process, state.ToArray()); } else { ThreadPool.QueueUserWorkItem(ProcessSequential, state.ToArray()); } ConfigurationFile.Current.Settings.General["Selection", true].Value = selection.ToString(); ConfigurationFile.Current.Save(); StopProcessing.Visible = true; StartProcessing.Visible = false; SplitContainerTop.Enabled = false; } else { ShowUpdateMessage("Connection timeout."); } } } catch (Exception ex) { ShowUpdateMessage("Error starting processing - {0}", ex.Message); } finally { StartProcessing.Enabled = true; this.Cursor = Cursors.Default; } }