方式一:一次獲取,異步寫(xiě)入
/// <summary>
/// 緩沖區(qū)大小
/// </summary>
public const int numpixels = 512 * 512;
/// <summary>
/// 將數(shù)據(jù)文件寫(xiě)入磁盤(pán)
/// </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]["附件名稱(chēng)"].tostring();
//記錄輸出列表
logproxy.writelist(strdirectory + filename);
//獲取文件數(shù)據(jù)
byte [] imagecontent = (byte[])objtable.rows[i]["附件內(nèi)容"];
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();
}
}
方式二:聯(lián)機(jī)讀取,異步寫(xiě)入
/// <summary>
/// 緩沖區(qū)大小
/// </summary>
public const int numpixels = 512 * 512;
/// <summary>
/// 將數(shù)據(jù)文件寫(xiě)入磁盤(pán)
/// </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;
//開(kāi)啟連接
try
{
cnn.open();
}
catch(exception err)
{
strerr = err.message;
return false;
}
byte[] pixels = new byte[numpixels];
oledbdatareader reader = cmd.executereader();
byte[]imagecontent;
//逐條處理
while(reader.read())
{
string filename = reader.getstring(1);
//記錄輸出列表
logproxy.writelist(strdirectory + filename);
//獲取文件數(shù)據(jù)
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();
//關(guān)閉連接
if(cnn.state == system.data.connectionstate.open)
{
cnn.close();
}
strerr = "";
//釋放資源
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();
}
}
兩種方式的比較:
方式一:適合于數(shù)據(jù)庫(kù)負(fù)載較大,二進(jìn)制數(shù)據(jù)大小已知的情況;
方式二:適合于數(shù)據(jù)庫(kù)負(fù)載較小,二進(jìn)制數(shù)據(jù)大小未知的情況;
其中:兩種方式的異步機(jī)制都是相同的,沒(méi)有任何區(qū)別;異步機(jī)制的優(yōu)點(diǎn)在于充分發(fā)揮了操作系統(tǒng)的優(yōu)點(diǎn)
注意:在需要對(duì)性能進(jìn)行同比測(cè)試的上下文中不能采用異步機(jī)制而必須盡量采用同步機(jī)制,以提高真實(shí)性