1

请注意,我正在使用 运行 Windows 服务应用程序.NET Framework 4.6.2,并且仅偶尔会出现此错误:

2019-04-22 18:35:36.7727|ERROR|DataIntegrator.MyService|ERROR: code = IoErr (10), message = System.Data.SQLite.SQLiteException (0x800007FF): disk I/O error
disk I/O error
   at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String& strRemain)
   at System.Data.SQLite.SQLiteCommand.BuildNextCommand()
   at System.Data.SQLite.SQLiteDataReader.NextResult()
   at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
   at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(CommandBehavior behavior)
   at System.Data.SQLite.SQLiteHelper.Insert(String tableName, Dictionary`2 dic) in C:\Projects\DataIntegrator\DataIntegrator\DataAccessLayer\SQLiteHelper.cs:line 254
   at System.Data.SQLite.Insert.InsertTag(Tag tag) in C:\Projects\DataIntegrator\DataIntegrator\DataAccessLayer\Query\Insert.cs:line 60

这是我的代码,错误发生在 sh.Insert 行:

    public static void InsertTag(Tag tag)
    {
        try
        {
            using (SQLiteConnection conn = new SQLiteConnection(Constants.DataSource))
            {
                using (SQLiteCommand cmd = new SQLiteCommand())
                {
                    cmd.Connection = conn;
                    conn.Open();

                    SQLiteHelper sh = new SQLiteHelper(cmd);

                    var dic = new Dictionary<string, object>();
                    dic["Id"] = tag.Id;
                    dic["Item"] = tag.Item;
                    dic["Value"] = tag.Value;
                    dic["Source"] = tag.Source;

                    sh.Insert(Constants.TagTable, dic);

                    conn.Close();
                }
            }
        }
        catch (Exception ex)
        {
            LogError("ERROR: {0}", ex.ToString());
        }
    }

有没有人有什么建议?以下是我检查过但尚未找到解决方案的其他一些链接:

错误代码为 10 的 SQLiteDiskIOException:磁盘 I/O 错误

cli上的sqlite3磁盘I/O错误

https://dba.stackexchange.com/questions/93575/sqlite-disk-io-error-3850

https://github.com/linuxserver/docker-sonarr/issues/38

https://forums.sonarr.tv/t/disk-io-and-sqllite-error/5578

在最后一个中,它提到数据库已损坏,但是当我停止控制台应用程序时,我可以打开数据库。我是否应该使用可能具有更好性能的其他数据库,例如 Berkeley DB?

https://www.oracle.com/technetwork/database/database-technologies/berkeleydb/downloads/index.html

http://www.tsjensen.com/blog/post/2011/09/03/How+To+Get+Berkeley+DB+5228+And+NET+Examples+Working+In+Visual+Studio+2010+SP1

更新:

将操作系统添加到标签

4

1 回答 1

0

请注意,我最初使用的是同步 DataReceived 事件处理程序,这导致 CPU 被大量使用并给我磁盘 I/O 错误:

    private readonly SerialPort _port = null;

    public MySerialPort(string comPortName)
    {
        ComPortName = comPortName;
        _port = new SerialPort(ComPortName,
            9600, Parity.None, 8, StopBits.One);
            _port.DataReceived -= new
                SerialDataReceivedEventHandler(port_DataReceived);
        _port.Open();
    }

    private void port_DataReceived(object sender,
        SerialDataReceivedEventArgs e)
    {
        SerialPort port = (SerialPort)sender;
        if (!port.IsOpen) return;

        for (int i = 0; i < port.BytesToRead; i++)
        {
            // process bytes
        }
    }

为了提高性能并解决 I/O 磁盘错误,我通过 Base 类更改为使用异步事件处理程序:

    private readonly SerialPort _port = null;

    public MySerialPort(string comPortName)
    {
        ComPortName = comPortName;
        _port = new SerialPort(ComPortName,
            9600, Parity.None, 8, StopBits.One);
         _port.Open();
         var rxData = Task.Run(async () => await ReceiveData());
        // ...
    }


public async Task<Stream> ReceiveData()
{
    var buffer = new byte[4096];
    int readBytes = 0;
    using (MemoryStream memoryStream = new MemoryStream())
    {
        while ((readBytes = await _port.BaseStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
        {
            memoryStream.Write(buffer, 0, readBytes);
        }

        return memoryStream;
    }

}

详情请看

http://www.sparxeng.com/blog/software/must-use-net-system-io-ports-serialport

Async SerialPort Read 的正确实现

于 2019-06-27T16:50:26.123 回答