Tasks
Front and rear streams:
the background thread does not support a running managed runtime. Once all the foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), the system stops all background threads and shuts down.
This also applies to threads from the thread pool, which are used by default when starting a new task without specifying another option:
Task planners
.NET Framework 4 ThreadPool
:
, ( , IsThreadPoolThread ) .
, . , , :
static void Main(string[] args)
{
Task.Factory.StartNew(Method);
Task.Delay(1000);
}
static void Method()
{
try
{
while (true) { }
}
finally
{
File.WriteAllText(@"C:\Polygon\Test.txt", "test");
}
}
, .
ThreadAbortException -
.NET , ThreadAbortException, , :
CLR , , Thread.Abort.
Process.Exited event -
, , :
var myProcess = new Process();
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(CleanUp);
.
AppDomain.CurrentDomain.ProcessExit -
, Avneesh, :
static void Main(string[] args)
{
AppDomain.CurrentDomain.ProcessExit += CleanUp;
Task.Factory.StartNew(Method);
Task.Delay(1000);
}
static void Method()
{
try
{
while (true) { }
}
finally
{
CleanUp(null, EventArgs.Empty);
}
}
static void CleanUp(object sender, EventArgs args)
{
File.WriteAllText(@"C:\Polygon\Test.txt", "test");
}
, rosources, :
:
- ExitProcess. , CRT (CRT) ExitProcess, .
- .
- TerminateProcess .
- ExitProcess, CTRL + C CTRL + BREAK.
- .
DB
- , , , . , .
#:
SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=[database_name];Integrated Security=True");
connection.Open();
Debugger.Launch();
connection.Close();
connection.Dispose();
T-SQL :
SELECT COUNT(*) FROM sys.dm_exec_sessions
WHERE nt_user_name = '[user_name]' AND program_name = '.Net SqlClient Data Provider'
, . , , . - , - , .