Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a simple example of how to use the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx" rel="nofollow noreferrer"><code>BackgroundWorker</code></a> as it applies to your code:</p> <pre><code>private void StartQuery(string query) { BackgroundWorker backgroundWorker1 = new BackgroundWorker(); backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); backgroundWorker1.RunWorkerAsync(query); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { e.Result = SQLGet((string)e.Argument); } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { queryResult = (string)e.Result; } </code></pre> <p>You may also wish to allow cancellation, provide error details, or provide robust feedback as it fetches data. Take a look at the example <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx" rel="nofollow noreferrer">on the MSDN page</a> for more details.</p> <p>The result of your query will show up in the <code>BackgroundWorker.RunWorkerCompleted</code> event as <code>e.Result</code> (I've stored it as an instance variable in this case). If you're going to run many of these at the same time, you'll need a way to differentiate which query is which. So you should pass more than just a string to the method. Take this example:</p> <pre><code>private int NextID = 0; private struct QueryArguments { public QueryArguments() { } public QueryArguments(int QueryID, string Query) : this() { this.QueryID = QueryID; this.Query = Query; } public int QueryID { get; set; } public string Query { get; set; } public string Result { get; set; } } private int StartQuery(string query) { QueryArguments args = new QueryArguments(NextID++, query); BackgroundWorker backgroundWorker1 = new BackgroundWorker(); backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); backgroundWorker1.RunWorkerAsync(args); return args.QueryID; } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { QueryArguments args = (QueryArguments)e.Argument; args.Result = SQLGet(args.Query); e.Result = args; } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { QueryArguments args = (QueryArguments)e.Result; //args.Result contains the result //do something } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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