So they told me what I'm doing wrong here, but I'm not sure why.
I have a web page that imports a CSV file with document numbers for an expensive operation. I put an expensive operation in the background thread to prevent it blocking the application. Here's what I have in a nutshell.
protected void ButtonUpload_Click(object sender, EventArgs e) { if (FileUploadCSV.HasFile) { string fileText; using (var sr = new StreamReader(FileUploadCSV.FileContent)) { fileText = sr.ReadToEnd(); } var documentNumbers = fileText.Split(new[] {',', '\n', '\r'}, StringSplitOptions.RemoveEmptyEntries); ThreadStart threadStart = () => AnotherClass.ExpensiveOperation(documentNumbers); var thread = new Thread(threadStart) {IsBackground = true}; thread.Start(); } }
(obviously with some error checking and messages for users)
So my three dimensional question:
- a) Is this a bad idea?
- b) Why is this a bad idea?
- c) What would you do instead?
multithreading c # background
Matt grande
source share