Asynchronously writes binary data from a database to disk

Source: Internet
Author: User
Tags bool count sleep thread
Binary | data | database | Asynchronous mode one: Fetch at a time, write asynchronously
<summary>
Buffer size
</summary>
public const INT NUMPIXELS = 512 * 512;
<summary>
Writing data files to disk
</summary>
<param name= "strSQL" ></param>
<returns></returns>
public static bool Makefilewithwritelistbyadapter (string Strsql,out string strerr)
{
if (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 + "\";
if (! Directory.Exists (strdirectory)) directory.createdirectory (strdirectory);
for (int i = 0;i< ObjTable.Rows.Count; i + +)
{

String fileName = objtable.rows[i]["attachment name"]. ToString ();
Record Output list
Logproxy.writelist (strdirectory + fileName);
Get File Data
byte [] imagecontent = (byte[]) objtable.rows[i]["attachment content"];
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 ();
}
Strerr = "";
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 ();
}
}

Mode two: Read online, asynchronous write

<summary>
Buffer size
</summary>
public const INT NUMPIXELS = 512 * 512;
<summary>
Writing data files to disk
</summary>
<param name= "strSQL" ></param>
<returns></returns>
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.Exists (strdirectory)) directory.createdirectory (strdirectory);
System.Data.OleDb.OleDbCommand cmd = new OleDbCommand ();
OleDbConnection Cnn = new OleDbConnection (Configproxy.getvaluebykey ("oleconnectionstring"));
Cmd. Connection = Cnn;
Cmd.commandtext = strSQL;
Open connection
Try
{
Cnn.open ();
}
catch (Exception ERR)
{
Strerr = Err.message;
return false;
}
byte[] pixels = new Byte[numpixels];
OleDbDataReader reader = cmd. ExecuteReader ();
Byte[]imagecontent;
Process-by-article
while (reader. Read ())
{
String fileName = reader. GetString (1);
Record 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 = "";
Releasing resources
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 ();
}
}

Comparison of two ways:

Mode one: Suitable for large database load, binary data size known;
Mode two: Suitable for the database load is small, binary data size is unknown;

Among them: The asynchronous mechanism of the two methods are the same, without any difference; The advantage of asynchronous mechanism is that it can give full play to the advantages of operating system
Note: Asynchronous mechanisms cannot be used in contexts where performance testing is required, and synchronization mechanisms must be used to improve authenticity





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.