// implement thread-safe messaging with delegate protected void FileBrowser_ProgressUpdate(object updateData) { if (this.InvokeRequired) { FileBrowser_ThreadPointer tp = new FileBrowser_ThreadPointer(FileBrowser_ProgressUpdate); this.Invoke(tp, new object[] { updateData }); } else { ThreadMessageStruct tms = (ThreadMessageStruct)updateData; ListViewItem item; switch (tms.Type) { case TMSType.Progress1: this.UpdateForm(tms); break; case TMSType.Progress2: break; case TMSType.Complete1: break; case TMSType.Complete2: break; case TMSType.Error: Exception ex = (Exception)tms.Data1; // thread.abort() throws an exception if (ex.GetType() == typeof(System.Threading.ThreadAbortException)) { MessageBox.Show("Thread aborted.", "Processing Canceled", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show(ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace, "Processing Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } //(Parent as Form).Close(); break; default: break; } // strange issue where if the sorting is run subsequent times, this method calls itself (updateData as ThreadMessageStruct).Type = TMSType.Unknown; } }
protected void FileBrowser_ProgressUpdate(object updateData) { if (this.InvokeRequired) { FileBrowser_ThreadPointer pc = new FileBrowser_ThreadPointer(FileBrowser_ProgressUpdate); this.Invoke(pc, new object[] { updateData }); } else { ThreadMessageStruct tms = (ThreadMessageStruct)updateData; ListViewItem item; switch (tms.Type) { case TMSType.Progress1: string tname = tms.Data1.ToString().Substring(f_rootPath.Text.Length); item = new ListViewItem(tname); item.SubItems.Add(""); f_filesResults.Items.Insert(0, item); m_filesChecked++; f_filesChecked.Text = "Files Checked: " + m_filesChecked; break; case TMSType.Progress2: if (f_filesResults.Items.Count > 0) /// what ?? { item = f_filesResults.Items[0]; string status = item.SubItems[1].Text; if (string.IsNullOrEmpty(status) == false) status += ", "; status += tms.Data2.ToString(); item.SubItems[1] = new ListViewItem.ListViewSubItem(item, status); } break; case TMSType.Complete1: break; case TMSType.Complete2: break; case TMSType.Error: // computation has thrown an error Exception ex = (Exception)tms.Data1; // thread.abort() throws an exception if (ex.GetType() == typeof(System.Threading.ThreadAbortException)) { MessageBox.Show("Thread aborted.", "Processing Canceled", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show(ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace, "Processing Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } //(Parent as Form).Close(); break; default: break; } } }