也許這對我來說是一個愚蠢的錯誤,但我無法弄清楚。C#線程:在不正確的對象上執行的線程
所以這種情況是我每200毫秒從數據庫中檢索一條記錄,如果有可用的話。 在每條記錄上,我開始一個線程。 在這種情況下,它是發送的郵件。
QMail mailRecord;
while (!stopSending)
{
if (QueueMailingHandler.m_numActive >= MaxThreads)
{
Thread.Sleep(2000);
continue;
}
mailRecord = QMail.Next();
if (mailRecord.UID > 0)
{
QueueMailingHandler.m_numActive++;
QueueMailingHandler MailingHandler = new QueueMailingHandler();
mailRecord.Processing = true;
MailingHandler.Start(mailRecord);
}
Thread.Sleep(200);
}
我MailingHandler:
internal QueueMailingHandler()
{
}
internal void Start(QMail rec)
{
record = rec;
Thread thread = new Thread(new ThreadStart(ProcessThread));
thread.IsBackground = true;
thread.Start();
}
public void ProcessThread()
{
_logging = new AutoQueueLog(record.UID.ToString(), "Sending Mails", record.Subject, "Processing");
_logging.Path = @"C:\Windows Services\QueueMailing\AutoLog";
_logging.LogMessage();
try
{
SendMail(record);
record.SetDone();
_logging.State = "Done";
_logging.LogMessage();
}
catch (Exception ex)
{
_logging.State = "Error";
_logging.LogException = ex;
_logging.Level = AutoLog.ExceptionLevel.Major;
_logging.LogMessage();
}
finally
{
m_numActive--;
}
}
而作爲loggingresult,我得到這個:
6/16/2015 11:57:02 AM - [328] - Function : QueueMailingHandler.ProcessThread() - Processing
6/16/2015 11:57:02 AM - [329] - Function : QueueMailingHandler.ProcessThread() - Processing
6/16/2015 11:57:02 AM - [329] - Function : QueueMailingHandler.ProcessThread() - Done
6/16/2015 11:57:02 AM - [330] - Function : QueueMailingHandler.ProcessThread() - Done
6/16/2015 11:57:02 AM - [330] - Function : QueueMailingHandler.ProcessThread() - Processing
6/16/2015 11:57:02 AM - [331] - Function : QueueMailingHandler.ProcessThread() - Processing
6/16/2015 11:57:02 AM - [331] - Function : QueueMailingHandler.ProcessThread() - Done
6/16/2015 11:57:03 AM - [332] - Function : QueueMailingHandler.ProcessThread() - Processing
6/16/2015 11:57:03 AM - [333] - Function : QueueMailingHandler.ProcessThread() - Processing
6/16/2015 11:57:03 AM - [333] - Function : QueueMailingHandler.ProcessThread() - Done
6/16/2015 11:57:03 AM - [333] - Function : QueueMailingHandler.ProcessThread() - Done
6/16/2015 11:57:03 AM - [333] - Function : QueueMailingHandler.ProcessThread() - Done
的問題是:爲什麼使用,應該在一個單獨的對象(但我的主題相似)線程?
任何幫助,歡迎。由於
所以你的問題是什麼? –
我很樂意提供幫助,但您的問題到底是什麼? –
我幾乎可以肯定,錯誤在你的'record = rec'行中。你想複製你的對象,但你只需複製一個引用到原來的引用,所以下一個'record = rec;'會覆蓋最後一個。你可以嘗試在'rec'上調用copy contructor嗎? – mg30rg