0

短いコードを作成したところ、次の例外が発生しました。

ハンドルは同期操作をサポートしていません。FileStream コンストラクターへのパラメーターを変更して、ハンドルが非同期で開かれた (つまり、オーバーラップ I/O のために明示的に開かれた) ことを示す必要がある場合があります。

コードは次のようなものです。

byte[] filebytes = File.ReadAllBytes(filepath);
List<byte> codedbytes = new List<byte>();
byte constbyte = 240;
int blocksize = 67108864;
int counter = 0;
List<int> tempindex = new List<int>();
int index = 0;

private void DoTheJob()
{

        [...]

        for (int i = 0; i < filebytes.Length; i++,counter++)
        {
            codedbytes.AddRange(BitConverter.GetBytes(Convert.ToChar((filebytes[i] * constbyte))));
            if (counter == blocksize)
            {
                FileStream tempwriter = File.Create(startdir + "\\temp" + index.ToString() + ".file");

                for (int x = 0; x < codedbytes.Count; x++)
                {
                    tempwriter.WriteByte(codedbytes[x]);
                }

                tempwriter.Close();
                codedbytes.Clear();
                counter = 0; tempindex.Add(index); index++;
            }
        }

        [...]

        for (int x = 0; x < tempindex.Count; x++)
        {
            ▉Exception at this line▉▶ codedbytes = new List<byte>(File.ReadAllBytes(startdir + "\\temp" + tempindex[x].ToString() + ".file"));

            [...]
        }
}

FileStrem クラスにはオーバーロードされたコンストラクターがなく、すべてのオブジェクトを閉じたため、何が問題なのかわかりません。

正しい道を教えてください!

4

1 に答える 1

0

問題は、FileStreamを閉じているだけで、FileStreamを破棄していないことだと思います。closeで解放されないが、Disposeで解放されるファイルに関連付けられたリソースがある可能性があると思います。例外が言及しているコンストラクターは、おそらくFile.ReadAllBytesを実行しようとしたときに作成されたFileStreamです。

これを試して:

private void DoTheJob()
{

        [...]

        for (int i = 0; i < filebytes.Length; i++,counter++)
        {
            codedbytes.AddRange(BitConverter.GetBytes(Convert.ToChar((filebytes[i] * constbyte))));
            if (counter == blocksize)
            {
                using (FileStream tempwriter = File.Create(startdir + "\\temp" + index.ToString() + ".file"))
                {
                    for (int x = 0; x < codedbytes.Count; x++)
                    {
                        tempwriter.WriteByte(codedbytes[x]);
                    }               
                }

                codedbytes.Clear();
                counter = 0; tempindex.Add(index); index++;
            }
        }

        [...]

        for (int x = 0; x < tempindex.Count; x++)
        {
            codedbytes = new List<byte>(File.ReadAllBytes(startdir + "\\temp" + tempindex[x].ToString() + ".file"));

            [...]
        }
}
于 2012-06-26T18:50:00.473 に答える