asynchronous - C# SqlClient asynch locking up program -


i've been having difficulty getting queries in c# run asynchronously. followed example beginexecutereader() on msdn, while query running, program's window frozen. want give idea program working , not frozen, thought begin/end methods let do. swapped out huge query simpler 1 readability here.

private void mysqlhandler() {     statuslabel.text = "connecting database...";     string constr = string.format("data source=server;initial catalog=master; integrated security=sspi");     sqlconnection con = new sqlconnection(constr);     //go ahead , set these     string commandstring = @"select top 1 * mytable";     sqlcommand command = new sqlcommand(commandstring, con);     //try block connecting database     try     {         con.open();         statuslabel.text = "querying database";         //try block querying database         try         {                                 command.commandtype = commandtype.text;             iasyncresult result = command.beginexecutereader();             //let user know stuff happening             while (!result.iscompleted)             {                 seconds++;                 int s = seconds % 60;                 int m = (int)math.floor((double)seconds / 60);                 querytimerlabel.text = string.format("{0:d2}:{1:d2}", m, s);                 switch (seconds % 3)                 {                     case 0:                         statuslabel.text = "querying database.";                         break;                     case 1:                         statuslabel.text = "querying database..";                         break;                     case 2:                         statuslabel.text = "querying database...";                         break;                 }                 system.threading.thread.sleep(1000); //so while loop doesn't take memory or             }             sqldatareader reader = command.endexecutereader(result); //get result             seconds = 0; //reset time query has taken             statuslabel.text = "query successful";         }         catch (exception err)         {             showerror(err);             statuslabel.text = "error querying database";         }     }     catch (exception err)     {         showerror(err);         statuslabel.text = "error connecting database";     }         {         if (con != null)             con.close();     } } 

you're in while loop , making thread sleep 1 second until (the query) completes. aren't doing asynchronously here.

you should executereaderasync() instead if can, because loop you're doing not going make asynchronous.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -