FileStream.BeginWrite メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > FileStream.BeginWrite メソッドの意味・解説 

FileStream.BeginWrite メソッド

非同期書き込み開始します

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Overrides Function
 BeginWrite ( _
    array As Byte(), _
    offset As Integer, _
    numBytes As Integer, _
    userCallback As AsyncCallback, _
    stateObject As Object _
) As IAsyncResult
Dim instance As FileStream
Dim array As Byte()
Dim offset As Integer
Dim numBytes As Integer
Dim userCallback As AsyncCallback
Dim stateObject As Object
Dim returnValue As IAsyncResult

returnValue = instance.BeginWrite(array, offset, numBytes, userCallback, stateObject)
public override IAsyncResult BeginWrite (
    byte[] array,
    int offset,
    int numBytes,
    AsyncCallback userCallback,
    Object stateObject
)
public:
virtual IAsyncResult^ BeginWrite (
    array<unsigned char>^ array, 
    int offset, 
    int numBytes, 
    AsyncCallback^ userCallback, 
    Object^ stateObject
) override
public IAsyncResult BeginWrite (
    byte[] array, 
    int offset, 
    int numBytes, 
    AsyncCallback userCallback, 
    Object stateObject
)
public override function BeginWrite (
    array : byte[], 
    offset : int, 
    numBytes : int, 
    userCallback : AsyncCallback, 
    stateObject : Object
) : IAsyncResult

パラメータ

array

現在のストリーム書き込むデータ格納しているバッファ

offset

現在のストリームへのバイトコピー開始する位置を示す array 内のバイト オフセットインデックス番号は 0 から始まります

numBytes

書き込む最大バイト数。

userCallback

非同期書き込み操作完了したときに呼び出されるメソッド

stateObject

この特定の非同期書き込み要求を他の要求区別するために使用するユーザー指定オブジェクト

戻り値
非同期書き込み参照する IAsyncResult。

例外例外
例外種類条件

ArgumentException

array から offset差し引いた値が numBytes より小さい値です。

ArgumentNullException

arraynull 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

offset または numBytes が負の値です。

NotSupportedException

ストリーム書き込みサポートしません。

ObjectDisposedException

ストリーム閉じられました。

IOException

I/O エラー発生しました

解説解説

EndWrite は、BeginWrite からの各 IAsyncResult ごとに必ず一度呼び出す必要がありますEndWriteI/O 操作完了するまでブロックします

このメソッドは、BeginWrite をオーバーライドます。

FileStream には、同期 I/O非同期 I/O2 種類操作モードありますどちらも使用できる場合に、基になるオペレーティング システム リソースはいずれかのモードでのアクセスだけを許可することがあります既定では、FileStream同期的オペレーティング システム ハンドル開きますWindows では、これによって非同期メソッド速度遅くなります非同期メソッド使用する場合は、FileStream(String,FileMode,FileAccess,FileShare,Int32,Boolean) コンストラクタ使用してください

ストリーム閉じている場合、または無効な引数渡した場合は、BeginWrite からすぐに例外スローさます。I/O 要求中のディスク障害など、非同期書き込み要求中に発生したエラーは、スレッド プールスレッド発生しEndWrite呼び出したときに可視なります

メモメモ

Windows では、64 KB 未満すべての I/O 操作同期的実行した方がパフォーマンス高くなります非同期 I/O は、64 KB 未満バッファ サイズでのパフォーマンス低下させることがあります

EndWrite は、読み取られたバイト数を検出するために、この IAsyncResult と共に呼び出す必要があります

複数非同期要求同時に実行した場合要求完了順序不定です。

その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します

使用例使用例

次のコード例は、FileStream(String,FileMode,FileAccess,FileShare,Int32,Boolean) コンストラクタ例の一部です。

Shared Sub Main()

    ' Create a synchronization object that gets 
    ' signaled when verification is complete.
    Dim manualEvent As New
 ManualResetEvent(False)

    ' Create random data to write to the file.
    Dim writeArray(100000) As Byte
    Dim randomGenerator As New
 Random()
    randomGenerator.NextBytes(writeArray)

    Dim fStream As New FileStream("Test#@@#.dat",
 _
        FileMode.Create, FileAccess.ReadWrite, _
        FileShare.None, 4096, True)

    ' Check that the FileStream was opened asynchronously.
    If fStream.IsAsync = True
        Console.WriteLine("fStream was opened asynchronously.")
    Else
        Console.WriteLine("fStream was not opened asynchronously.")
    End If

    ' Asynchronously write to the file.
    Dim asyncResult As IAsyncResult = fStream.BeginWrite(
 _
        writeArray, 0, writeArray.Length, _
        AddressOf EndWriteCallback , _
        New State(fStream, writeArray, manualEvent))

    ' Concurrently do other work and then wait
    ' for the data to be written and verified.
    manualEvent.WaitOne(5000, False)
End Sub
static void Main()
{
    // Create a synchronization object that gets 
    // signaled when verification is complete.
    ManualResetEvent manualEvent = new ManualResetEvent(false);

    // Create random data to write to the file.
    byte[] writeArray = new byte[100000];
    new Random().NextBytes(writeArray);

    FileStream fStream = 
        new FileStream("Test#@@#.dat", FileMode.Create,
 
        FileAccess.ReadWrite, FileShare.None, 4096, true);

    // Check that the FileStream was opened asynchronously.
    Console.WriteLine("fStream was {0}opened asynchronously.",
        fStream.IsAsync ? "" : "not ");

    // Asynchronously write to the file.
    IAsyncResult asyncResult = fStream.BeginWrite(
        writeArray, 0, writeArray.Length, 
        new AsyncCallback(EndWriteCallback), 
        new State(fStream, writeArray, manualEvent));

    // Concurrently do other work and then wait 
    // for the data to be written and verified.
    manualEvent.WaitOne(5000, false);
}
int main()
{
   
   // Create a synchronization object that gets 
   // signaled when verification is complete.
   ManualResetEvent^ manualEvent = gcnew ManualResetEvent( false
 );
   
   // Create the data to write to the file.
   array<Byte>^writeArray = gcnew array<Byte>(100000);
   (gcnew Random)->NextBytes( writeArray );
   FileStream^ fStream = gcnew FileStream(  "Test#@@#.dat",FileMode::Create,FileAccess::ReadWrite,FileShare::None,4096,true
 );
   
   // Check that the FileStream was opened asynchronously.
   Console::WriteLine( "fStream was {0}opened asynchronously.", fStream->IsAsync
 ? (String^)"" : "not " );
   
   // Asynchronously write to the file.
   IAsyncResult^ asyncResult = fStream->BeginWrite( writeArray, 0, writeArray->Length,
 gcnew AsyncCallback( &FStream::EndWriteCallback ), gcnew State( fStream,writeArray,manualEvent
 ) );
   
   // Concurrently do other work and then wait 
   // for the data to be written and verified.
   manualEvent->WaitOne( 5000, false );
}

public static void main(String[]
 args)
{
    // Create a synchronization object that gets 
    // signaled when verification is complete.
    ManualResetEvent manualEvent = new ManualResetEvent(false);

    // Create random data to write to the file.
    ubyte writeArray[] = new ubyte[100000];
    new Random().NextBytes(writeArray);

    FileStream fStream =  new FileStream("Test#@@#.dat",
 FileMode.Create,
        FileAccess.ReadWrite, FileShare.None, 4096, true);

    // Check that the FileStream was opened asynchronously.
    Console.WriteLine("fStream was {0}opened asynchronously.",
        (fStream.get_IsAsync()) ? "" : "not ");
    FStream classfStream = new FStream();

    // Asynchronously write to the file.
    IAsyncResult asyncResult = fStream.BeginWrite(writeArray, 0,
        writeArray.length, new AsyncCallback(EndWriteCallback)
,
        classfStream.new State(fStream, writeArray, manualEvent));

    // Concurrently do other work and then wait 
    // for the data to be written and verified.
    manualEvent.WaitOne(5000, false);
} //main
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

FileStream.BeginWrite メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



FileStream.BeginWrite メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS