国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

將數(shù)據(jù)庫(kù)中二進(jìn)制數(shù)據(jù)以異步方式寫(xiě)入磁盤(pán)

2024-07-21 02:22:55
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
方式一:一次獲取,異步寫(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í)性




發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 四会市| 巴青县| 台南市| 威远县| 五指山市| 涟源市| 扶沟县| 闽清县| 呼伦贝尔市| 宽城| 湘乡市| 高淳县| 鲁甸县| 皮山县| 万荣县| 岳西县| 重庆市| 延庆县| 洛浦县| 崇州市| 炉霍县| 错那县| 晋宁县| 垫江县| 新乡市| 海林市| 武陟县| 革吉县| 孝义市| 青铜峡市| 西吉县| 蓝山县| 杭锦后旗| 陵川县| 潼关县| 荥阳市| 同仁县| 福泉市| 文山县| 华容县| 元谋县|