Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti Threading with LINQ to SQL
    primarykey
    data
    text
    <p>I am writing a WinForms application. I am pulling data from my database, performing some actions on that data set and then plan to save it back to the database. I am using LINQ to SQL to perform the query to the database because I am only concerned with 1 table in our database so I didn't want to implement an entire ORM for this.</p> <p>I have it pulling the dataset from the DB. However, the dataset is rather large. So currently what I am trying to do is separate the dataset into 4 relatively equal sized lists (<code>List&lt;object&gt;</code>).</p> <p>Then I have a separate background worker to run through each of those lists, perform the action and report its progress while doing so. I have it planned to consolidate those sections into one big list once all 4 background workers have finished processing their section.</p> <p>But I keep getting an error while the background workers are processing their unique list. Do the objects maintain their tie to the DataContext for the LINQ to SQL even though they have been converted to List objects? Any ideas how to fix this? I have minimal experience with multi-threading so if I am going at this completely wrong, please tell me.</p> <p>Thanks guys. If you need any code snippets or any other information just ask.</p> <p><strong>Edit:</strong> Oops. I completely forgot to give the error message. In the DataContext designer.cs it gives the error <code>An item with the same key has already been added.</code> on the <code>SendPropertyChanging</code> function.</p> <pre><code>private void Setup(){ List&lt;MyObject&gt; quarter1 = _listFromDB.Take(5000).ToList(); bgw1.RunWorkerAsync(); } private void bgw1_DoWork(object sender, DoWorkEventArgs e){ e.Result = functionToExecute(bgw1, quarter1); } private List&lt;MyObject&gt; functionToExecute(BackgroundWorker caller, List&lt;MyObject&gt; myList) { int progress = 0; foreach (MyObject obj in myList) { string newString1 = createString(); obj.strText = newString; //report progress here caller.ReportProgress(progress++); } return myList; } </code></pre> <p>This same function is called by all four workers and is given a different list for myList based on which worker is called the function.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload