Write the binary data in asynchronously in an asynchronous manner

xiaoxiao2021-03-06  39

Method 1: Once acquired, asynchronously write ///

/// buffer size /// public const Int numpixels = 512 * 512; /// /// write data file Into-Disk /// /// /// public static bool makefilewithWritelistByadapter (String strsql, out string strand) {ix (file .Exists (ConfigProxy.GetValueByKey ( "ListFile"))) File.Delete (ConfigProxy.GetValueByKey ( "ListFile")); DataTable objTable; if (OleDataBaseProxy.ExecuteSql (strSql, out objTable, out strErr!)) return false; string outputPath = ConfigProxy.GetValueByKey ( "OutputPath"); if (objTable.Rows.Count <1) return false; string strDirectory = outputPath "//"; (! Directory.Exists (strDirectory)) if Directory.CreateDirectory (strDirectory) For (int i = 0; i

Public State (FileStream Fstream, AutoStevent Autoevent) {this.fstream = fstream; this.autoevent = autoevent;}} static void endwritecallback

State stateInfo = (State) asyncResult.AsyncState; int workerThreads; int portThreads; try {ThreadPool.GetAvailableThreads (out workerThreads, out portThreads); stateInfo.fStream.EndWrite (asyncResult); Thread.Sleep (1500);} finally {stateInfo. Autoevent.set ();}} method 2: online reading, asynchronous write ///

/// buffer size /// public constinary in //// < Summary> /// Write data files ///////// /// /// public static bool makefilewithWritelistByreader (String Strsql) , out string strErr) {if (File.Exists (ConfigProxy.GetValueByKey ( "ListFile"))) File.Delete (ConfigProxy.GetValueByKey ( "ListFile")); string outputPath = ConfigProxy.GetValueByKey ( "OutputPath"); string strDirectory = outputPath "//"; if Directory.CreateDirectory (strDirectory) (Directory.Exists (strDirectory)!); System.Data.OleDb.OleDbCommand cmd = new OleDbCommand (); OleDbConnection Cnn = new OleDbConnection (ConfigProxy .GetValuebyKey ("OleConnectionstring")); cmd.connection = cnn; cmd.commandtext = strsql; // Turn on the connection try {cnn.open ();} catch (exception err) {strr = err.Message; returnaf false;} Byte [] pixels = new byte [numpixels]; OLEDBDATAREADER Reader = cmd.executeRead (); Byte [] imagecontent; // Processes while (Reader.Read ()) {string filename = reader.getstring (1); // Record the output list logProxy.writelist (strdirectory filename); // Get file data imagecontent = new byte [convert.Toint64 (Reader.getstring (7))];

reader.GetBytes (6,0, ImageContent, 0, Convert.ToInt32 (reader.GetString (7))); AutoResetEvent manualEvent = new AutoResetEvent (false); FileStream fStream = new FileStream (strDirectory fileName, FileMode.Create, FileAccess. ReadWrite, FileShare.None, 4096, true); IAsyncResult asyncResult = fStream.BeginWrite (ImageContent, 0, ImageContent.Length, new AsyncCallback (EndWriteCallback), new State (fStream, manualEvent)); manualEvent.WaitOne (5000, false); FSTREAM.CLOSE ();} reader.close (); // Close connection IF (cnn.state == system.data.connectionState.open) {cnn.close ();} strerr = "; // Release Resource CNN .Dispose (); cmd.Dispose (); GC.Collect (); return true;} class State {public FileStream fStream; public AutoResetEvent autoEvent; public State (FileStream fStream, AutoResetEvent autoEvent) {this.fStream = fStream; this. Autoevent = autoevent;}} static void endwritecallback (iasyncResult asyncRESULT) {

State stateInfo = (State) asyncResult.AsyncState; int workerThreads; int portThreads; try {ThreadPool.GetAvailableThreads (out workerThreads, out portThreads); stateInfo.fStream.EndWrite (asyncResult); Thread.Sleep (1500);} finally {stateInfo. AUTOEVENT.SET ();}} Two ways to compare: mode 1: Suitable for the database load, binary data size is known; mode 2: Suitable for unknown on the database load and binary data size; : The asynchronous mechanism of two ways is the same, without any difference; the asynchronous mechanism is the advantage of fully utilizing the operating system Note: In the context of the performance of performance, the asynchronous mechanism must be used to use synchronization mechanism To improve authenticity

转载请注明原文地址:https://www.9cbs.com/read-52519.html

New Post(0)