I have an application that has two threads.
The first one (the main thread) that captures the data using socket and update DataTables
The second Inserts the DataTables into the database.
The application works fine but when it closes, the main thread finishes reading the data and call Abort method in the second thread, which may be inserting into database and this leads to inconsistent data.
Currently I am using the following solution to overcome "aborting during insertion"
EDIT: After the powerful answers I changed the code
void MainThread()
{
while(Read())
{
//Read Data through socket
try
{
//Wait on Mutex1
//Update Tables
}
finally
{
//Release Mutex1
}
}
_isrunning = false;
_secondThread.Join();
}
void SecondThread()
{
while(_isrunning)
{
try
{
//Wait on Mutex1
//Insert Tables into Database using transactions
}
finally
{
//Release Mutex1
}
}
}