0

这可能是一个新手问题,所以我会马上出来说。这是我第一次创建 IDisposable 类,我想确保我正确地创建了我的类,正确地调用它,并正确地处理它。谢谢!

使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间爬虫
{
    类 LoggingClass
    {
        公共类 LoggingDisposeable : IDisposable
        {
            public void GenericLogging(string systemMsg, string SystemClass, string SystemSection, string ID, string FixID, string baseURL, string mysqlQueryName, string mysqlQuery)
            {
                字符串 Loggingall = " 插入 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 = '自动交易者'" +
                                ",TimeStamp = Now()";
                使用 (var MYSQLP = new MySQLProcessing.MySQLProcessor())
                {
                    MYSQLP.MySQLInsertUpdate(Loggingall, "Loggingall");
                }
            }

            公共无效处置()
            {
                this.Dispose(true);
                GC.SuppressFinalize(this);
            }

            protected virtual void Dispose(bool disposing)
            {
                if (!this.Disposed)
                {
                    如果(处置)
                    {
                        // 在此处执行托管清理。

                    }

                    // 在此处执行非托管清理。

                    this.Disposed = true;
                }
            }

            受保护的布尔处置{得到; 私人套装;}

        }
    }
}

这就是我所说的。

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>
4

1 回答 1

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, "", "");
        });
}
于 2012-04-06T02:19:40.453 回答