这可能是一个新问题,所以我会马上说出来。这是我第一次创建一个IDisposable类,我想确保我正确创建了我的类,正确调用它,并正确处理它。谢谢!
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Crawler { class LoggingClass { public class LoggingDisposeable : IDisposable { public void GenericLogging(string systemMsg, string SystemClass, string SystemSection, string ID, string FixID, string baseURL, string mysqlQueryName, string mysqlQuery) { string Loggingall = " insert into tblLogs " + "set SystemMsg='" + systemMsg.Replace("'", "''") + "'" + ",SystemClass = '" + SystemClass.Replace("'", "''") + "'" + ",SystemSection = '" + SystemSection.Replace("'", "''") + ",ID = '" + CarID.Replace("'", "''") + "'" + ",FixID = '" + FixID.Replace("'", "''") + "'" + ",baseurl = '" + baseURL.Replace("'", "''") + "'" + ",mysqlqueryName = '" + mysqlQuery.Replace("'", "''") + ",mysqlquery = '" + mysqlQuery.Replace("'", "''") + "'" + ",Site = 'AutoTrader'" + ",TimeStamp = Now()"; using (var MYSQLP = new MySQLProcessing.MySQLProcessor()) { MYSQLP.MySQLInsertUpdate(Loggingall, "Loggingall"); } } public void Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!this.Disposed) { if (disposing) { // Perform managed cleanup here. } // Perform unmanaged cleanup here. this.Disposed = true; } } protected bool Disposed { get; private set; } } } }
这就是我所说的。
var options = new ParallelOptions();
options.MaxDegreeOfParallelism = 5;
Parallel.ForEach(urlTable.AsEnumerable(),options, drow =>
{
using (var logClass = new LoggingClass.LoggingDisposeable())
{
logClass.GenericLogging("ZipCode not available for this dealerL", "DealershipRequest", "checkExistingDealers", dealerID, "DealerShipZipCode",rDealerLink, "", "");
}
}</pre>
答案 0 :(得分:2)
我认为你打算做的是这样的事情:
public class LoggingDisposeable : IDisposable
{
MySQLProcessing MySQLP;
public LoggingDisposeable()
{
MYSQLP = new MySQLProcessing.MySQLProcessor();
}
public void GenericLogging(string systemMsg, string SystemClass, string SystemSection, string ID, string FixID, string baseURL, string mysqlQueryName, string mysqlQuery)
{
string Loggingall = " insert into tblLogs " +
"set SystemMsg='" + systemMsg.Replace("'", "''") + "'" +
",SystemClass = '" + SystemClass.Replace("'", "''") + "'" +
",SystemSection = '" + SystemSection.Replace("'", "''") +
",ID = '" + CarID.Replace("'", "''") + "'" +
",FixID = '" + FixID.Replace("'", "''") + "'" +
",baseurl = '" + baseURL.Replace("'", "''") + "'" +
",mysqlqueryName = '" + mysqlQuery.Replace("'", "''") +
",mysqlquery = '" + mysqlQuery.Replace("'", "''") + "'" +
",Site = 'AutoTrader'" +
",TimeStamp = Now()";
MYSQLP.MySQLInsertUpdate(Loggingall, "Loggingall");
}
public void Dispose()
{
MYSQLP.Dispose();
}
}
然后像这样使用它:
using (var logClass = new LoggingDisposeable())
{
var options = new ParallelOptions();
options.MaxDegreeOfParallelism = 5;
Parallel.ForEach(urlTable.AsEnumerable(), options, drow =>
{
logClass.GenericLogging("ZipCode not available for this dealerL", "DealershipRequest", "checkExistingDealers", dealerID, "DealerShipZipCode", rDealerLink, "", "");
});
}