M
magic man via .NET 247
hi everyone i have a c# application that uses multithreading toconnect to sql server and execute a stored procedure on theserver. i am using a dataset,sqlcommand,dataadapter and adatagrid to carry out the process on a background thread andeverything goes well but the problem arrises when i created astop button that attempts to cancel the operation of datasetfilling(somtimes the query takes much time and i need to cancelthe operation)
so that is what i did : 1)created global boolean variableisconnected
put the following code inside stopbutton_click
try
{ dataGrid1.DataSource =null; isconnected=false; mythread.Abort();
try
{
if (sqcmd != null)
{ sqcmd.Cancel (); }
if (sqlconn != null)
{ sqlconn.Close(); }
}
catch(Exception)
{return ;}
}
catch (ThreadAbortException)
{
return;
}
myform.button1 .Enabled =true;
myform.button2 .Enabled =true;
and that is the part of code that fills tha dataset (which isrunning in the background thread)
bool IsConnecting = true;
while(IsConnecting == true)
{
try
{
sqlconn.Open ();
myform.sqcmd = new SqlCommand ("sp_search_master",sqlconn);}
sda =new SqlDataAdapter (myform.sqcmd);
dssearch = new DataSet ();
myform.sqcmd.CommandType = CommandType.StoredProcedure ;
myform.sqcmd.Parameters.Add(new SqlParameter("@First_Part",SqlDbType.VarChar)).Value = myform.txtname.Text;
myform.sqcmd.CommandTimeout=0;
sda.Fill (dssearch, "igmaster"); myform.dtSource =dssearch.Tables["igmaster"];
IsConnecting = false; myform.statusBarPanel1 .Text =" searchended " ;
myform.sqcmd.Dispose (); sqlconn.Close ();
}
catch
{
MessageBox.Show("connection eror", "Application Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
the problm is that when i press stop button either theapplication hangs or VS gives me runtime error :unknow error
could u plz tel me a good practise for cancelling the job ofdataset filling smoothly without any problems
i have read somthing like that herehttp://www.codeproject.com/csharp/workerthread.asp#xx427611xxbut in that example there is a loop that checks the state of anevent how can also i override the exception peacefully ifthe button is pressed thans for ur time i am waiting ur help
so that is what i did : 1)created global boolean variableisconnected
put the following code inside stopbutton_click
try
{ dataGrid1.DataSource =null; isconnected=false; mythread.Abort();
try
{
if (sqcmd != null)
{ sqcmd.Cancel (); }
if (sqlconn != null)
{ sqlconn.Close(); }
}
catch(Exception)
{return ;}
}
catch (ThreadAbortException)
{
return;
}
myform.button1 .Enabled =true;
myform.button2 .Enabled =true;
and that is the part of code that fills tha dataset (which isrunning in the background thread)
bool IsConnecting = true;
while(IsConnecting == true)
{
try
{
sqlconn.Open ();
myform.sqcmd = new SqlCommand ("sp_search_master",sqlconn);}
sda =new SqlDataAdapter (myform.sqcmd);
dssearch = new DataSet ();
myform.sqcmd.CommandType = CommandType.StoredProcedure ;
myform.sqcmd.Parameters.Add(new SqlParameter("@First_Part",SqlDbType.VarChar)).Value = myform.txtname.Text;
myform.sqcmd.CommandTimeout=0;
sda.Fill (dssearch, "igmaster"); myform.dtSource =dssearch.Tables["igmaster"];
IsConnecting = false; myform.statusBarPanel1 .Text =" searchended " ;
myform.sqcmd.Dispose (); sqlconn.Close ();
}
catch
{
MessageBox.Show("connection eror", "Application Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
the problm is that when i press stop button either theapplication hangs or VS gives me runtime error :unknow error
could u plz tel me a good practise for cancelling the job ofdataset filling smoothly without any problems
i have read somthing like that herehttp://www.codeproject.com/csharp/workerthread.asp#xx427611xxbut in that example there is a loop that checks the state of anevent how can also i override the exception peacefully ifthe button is pressed thans for ur time i am waiting ur help