stop thread operations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • magic man via .NET 247

    stop thread operations

    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,sqlcomm and,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 variableisconne cted
    put the following code inside stopbutton_clic k
    try
    { dataGrid1.DataS ource =null; isconnected=fal se; mythread.Abort( );
    try
    {
    if (sqcmd != null)
    { sqcmd.Cancel (); }

    if (sqlconn != null)
    { sqlconn.Close() ; }
    }
    catch(Exception )
    {return ;}
    }

    catch (ThreadAbortExc eption)
    {
    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(IsConnect ing == true)
    {
    try
    {
    sqlconn.Open ();
    myform.sqcmd = new SqlCommand ("sp_search_mas ter",sqlconn); }
    sda =new SqlDataAdapter (myform.sqcmd);
    dssearch = new DataSet ();
    myform.sqcmd.Co mmandType = CommandType.Sto redProcedure ;
    myform.sqcmd.Pa rameters.Add(ne w SqlParameter("@ First_Part",Sql DbType.VarChar) ).Value = myform.txtname. Text;
    myform.sqcmd.Co mmandTimeout=0;
    sda.Fill (dssearch, "igmaster") ; myform.dtSource =dssearch.Table s["igmaster"];
    IsConnecting = false; myform.statusBa rPanel1 .Text =" searchended " ;
    myform.sqcmd.Di spose (); sqlconn.Close ();
    }
    catch
    {
    MessageBox.Show ("connection eror", "Applicatio n Error",MessageB oxButtons.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.as p#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
    --------------------------------
    From: magic man

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>dVVaZpRjbke 65E3LDYgTdw==</Id>
  • Adam Calderon [MCSD, MCAD, MCP]

    #2
    Re: stop thread operations

    It is hard to tell by the code fragement but here you go.

    Try and remove mythread.Abort from the click event handler and see if
    you can get the sqcmd.Cancel() to stop the stored procedure. Run SQL
    Profiler before you start your debug session and see if you are
    successful. Remeber that when calling sqlcmd.Cancel() you are not
    guarenteed that the current command will actually cancel and you will
    receive no exception if the call fails.

    If the sqcmd.Cancel() succeeds then the sda.Fill(dsSear ch, "igmaster")
    call in the thread prodedure should return and go into the catch
    with an exception and finally return.


    If this doesn't help you then try to put all database access code
    including your connection and command objects in the scope of the
    thread procedure only. Then try andt call just the mythread.Abort in
    the click event and see if this works. The logic here is that maybe
    with your variables being referenced in two different threads UI and
    the background thread you are experiencing some problems with
    referencing.


    Also for future reference you need to access your UI stuff differently
    using the BeginInvoke sytle that was described in the article you
    refered to.

    For reference look at this series of articles and see if they help.






    On Sat, 28 May 2005 09:06:36 -0700, magic man via .NET 247
    <anonymous@dotn et247.com> wrote:
    [color=blue]
    >hi everyone i have a c# application that uses multithreading to connect to sql server and execute a stored procedure on the server. i am using a dataset,sqlcomm and,dataadapter and a datagrid to carry out the process on a background thread and everything goes well but the problem arrises when i created a stop button that attempts to cancel the operation of dataset filling(somtime s the query takes much time and i need to cancel the operation)
    >so that is what i did : 1)created global boolean variable isconnected
    >put the following code inside stopbutton_clic k
    > try
    > { dataGrid1.DataS ource =null; isconnected=fal se; mythread.Abort( );
    > try
    > {
    > if (sqcmd != null)
    > { sqcmd.Cancel (); }
    >
    > if (sqlconn != null)
    > { sqlconn.Close() ; }
    > }
    > catch(Exception )
    > {return ;}
    > }
    >
    > catch (ThreadAbortExc eption)
    > {
    > return;
    > }
    > myform.button1 .Enabled =true;
    > myform.button2 .Enabled =true;
    >
    >and that is the part of code that fills tha dataset (which is running in the background thread)
    >
    >bool IsConnecting = true;
    > while(IsConnect ing == true)
    > {
    > try
    > {
    > sqlconn.Open ();
    > myform.sqcmd = new SqlCommand ("sp_search_mas ter",sqlconn); }
    > sda =new SqlDataAdapter (myform.sqcmd);
    > dssearch = new DataSet ();
    > myform.sqcmd.Co mmandType = CommandType.Sto redProcedure ;
    > myform.sqcmd.Pa rameters.Add(ne w SqlParameter("@ First_Part", SqlDbType.VarCh ar)).Value = myform.txtname. Text;
    > myform.sqcmd.Co mmandTimeout=0;
    > sda.Fill (dssearch, "igmaster") ; myform.dtSource = dssearch.Tables["igmaster"];
    > IsConnecting = false; myform.statusBa rPanel1 .Text =" search ended " ;
    > myform.sqcmd.Di spose (); sqlconn.Close ();
    > }
    > catch
    > {
    > MessageBox.Show ("connection eror", "Applicatio n Error", MessageBoxButto ns.OK, MessageBoxIcon. Error);
    > return;
    > }
    >the problm is that when i press stop button either the application hangs or VS gives me runtime error :unknow error
    >could u plz tel me a good practise for cancelling the job of dataset filling smoothly without any problems
    >i have read somthing like that here http://www.codeproject.com/csharp/wo...asp#xx427611xx but in that example there is a loop that checks the state of an event how can also i override the exception peacefully if the button is pressed thans for ur time i am waiting ur help
    >--------------------------------
    >From: magic man
    >
    >-----------------------
    >Posted by a user from .NET 247 (http://www.dotnet247.com/)
    >
    ><Id>dVVaZpRjbk e65E3LDYgTdw==</Id>[/color]

    Comment

    Working...