Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The major reason to make an application or service multithreaded is to perform database or other background operations without blocking (i.e. hanging) a presentation element like a Windows form. If your service depends on very rapid polling or expects db inserts to take a very long time, it might make sense to use two threads. But I can't imagine that either would be the case in your scenario.</p> <p>If you do decide to make your service multithreaded, the two major classes in C# that you want to look into are <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx" rel="nofollow" title="BackgroundWorker Class">BackgroundWorker</a> and <a href="http://msdn.microsoft.com/en-us/library/0ka9477y.aspx" rel="nofollow" title="The Managed Thread Pool">ThreadPool</a>. If you want to do multiple concurrent db inserts (for example, if you want to execute an insert for each of multiple RSS feeds polled at the same time), you should use a ThreadPool. Otherwise, use a BackgroundWorker.</p> <p>Typically, you'd have a db access class that would have a method to insert a row. That method would create a background worker, add DoWork handler to some static method in that db access class to the background worker, then call DoWorkAsync. You should only have db connection settings in that one class in order to make maintaining the code easier. For example:</p> <pre><code>public static class DbAccess { public void InsertRow(SomeObject entity) { BackgroundWorker bg = new BackgroundWorker(); bg.DoWork += InsertRow_DoWork; bg.RunWorkerCompleted += InsertRow_RunWorkerCompleted; bg.RunWorkerAsync(entity); } private void InsertRow_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker bg = sender as BackgroundWorker; SomeObject entity = e.Argument as SomeObject; // insert db access here } private void InsertRow_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // send notifications // alternatively, pass the InsertRow method a // delegate to a method in the calling class that will notify } } </code></pre>
 

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