473,624 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>
Nov 17 '05 #1
1 2240
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.

http://msdn.microsoft.com/library/de...ms06112002.asp
http://msdn.microsoft.com/library/de...ms08162002.asp
http://msdn.microsoft.com/library/de...ms08162002.asp
On Sat, 28 May 2005 09:06:36 -0700, magic man via .NET 247
<an*******@dotn et247.com> wrote:
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>


Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2407
by: Harald Armin Massa | last post by:
I need to do some synchronisations like in a cron.job import time from threading import Thread class updater(Thread): def run(self): while True: do_updates() time.sleep(600)
4
1763
by: Prasad | last post by:
Hi, Is it generally acceptable that the SCM says that the service has stopped but the service has spawn a thread which is still doing the cleanup thing which keeps the process running.
4
5427
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the server. Data is sent and received over these connections using the asynchronous model. The server is currently in beta testing. Sporadically over the course of the day, I'll observe the thread count on the process (via perfmon) start climbing....
11
2236
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe." I have 2 questions: 1. I thought dynamic variables are thread-safe since threads have their own
51
54812
by: Hans | last post by:
Hi all, Is there a way that the program that created and started a thread also stops it. (My usage is a time-out). E.g. thread = threading.Thread(target=Loop.testLoop) thread.start() # This thread is expected to finish within a second
9
7319
by: semedao | last post by:
Hi, I am using sync and async operations on the same socket. generally I want the socket to wait on BeginReceive and to not block the object thread. but in some cases I want to stop the BeginReceive in the middle - Don't accept any data from it , and using regular Receive (I don't want the data will come to the BeginReceive byte buffer , instead of other buffer) then when I comlete some operaion , to return and call to the BeginReceive...
6
3132
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it to the client. Before adding data to the arraylist, I check if the depth of the arraylist is longer than iMaxQueueDepth, and if it is, I clear the arraylist. Is it possible that while I am clearing the arraylist, the ThreadMain at the same time...
7
7673
by: Marc Bartsch | last post by:
Hi, I have a background worker in my C# app that makes a synchronous HttpWebRequest.GetResponse() call. The idea is to POST a file to a server on the internet. When I call HttpWebRequest.Abort() on the request object on another thread, GetResponse() returns with an exception as expected. However, when I monitor the network traffic, it does not seem to stop, but to continue to be active and to upload the file. The network is active even...
12
1677
by: Peter K | last post by:
Say I have this class public class Simple { private string name; public string Name { get { return name; } set { name = value; }
0
8242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8681
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8629
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7170
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.