Код: Выделить всё
public class CardHandligSpeedometer : IDisposable
{
private Thread m_Thread;
public CardHandligSpeedometer()
{
createThread();
}
public void StartThread()
{
m_IsStopped = false;
if (m_Thread == null)
{
createThread();
}
m_Thread.Start();
}
public void StopThread()
{
m_IsStopped = true;
if (m_Thread != null)
{
m_Thread.Join(500);
if (m_Thread != null && m_Thread.IsAlive)
{
m_Thread.Abort();
}
m_Thread = null;
}
}
public void Dispose()
{
StopThread();
}
void getHandledCard()
{
while (!m_IsStopped)
{
try
{
using (SqlConnection connection = new SqlConnection(LetFlowLib.FactoryData.StrCnt))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("GetHandledCardCountFor2Hours", connection))
{
//code
}
Thread.Sleep(m_Interval);
}
catch (Exception ex)
{
//code
}
}
}
private void createThread()
{
m_Thread = new Thread(getHandledCard);
m_Thread.SetApartmentState(ApartmentState.MTA);
m_Thread.IsBackground = true;
}
}
Код: Выделить всё
m_CardHandlingSpeedometer = gcnew ltr_GUI::CardHandligSpeedometer(_CTX.GetAppointID(), 30000);
m_CardHandlingSpeedometer->StartThread();
Если GC::Collect(2); и GC::WaitForPendingFinalizers(); вызываются в основном потоке, программа зависает на методе WaitForPendingFinalizers.
Но если в методе создания createThread() поменять SATA на MTA, то все работает нормально.
В чем причина?
Подробнее здесь: https://stackoverflow.com/questions/792 ... ion-thread
Мобильная версия