我正在做一個程序在c#(單聲道)打印到財務打印機(escpos),它工作正常。問題是,當我打印時,程序掛起,直到我有的緩衝區被清除。所以,如你所想象的,如果我打印一些圖像,它會變得更大,所以它會暫停一段時間。這是不可取的。非阻塞io使用BinaryWriter寫入到usblp0
BinaryWriter outBuffer;
this.outBuffer = new BinaryWriter(new FileStream (this.portName,System.IO.FileMode.Open));
.... apend bytes to buffer...
IAsyncResult asyncResult = null;
asyncResult = outBuffer.BaseStream.BeginWrite(buffer,offset,count,null,null);
asyncResult.AsyncWaitHandle.WaitOne(100);
outBuffer.BaseStream.EndWrite(asyncResult); // Last step to the 'write'.
if (!asyncResult.IsCompleted) // Make sure the write really completed.
{
throw new IOException("Writte to printer failed.");
}
第二種方式:我已經在兩個方面
一種方式測試
BinaryWriter outBuffer;
this.outBuffer = new BinaryWriter(new FileStream (this.portName,System.IO.FileMode.Open));
.... apend bytes to buffer...
outBuffer.Write(buffer, 0, buffer.Length);
既不方法允許程序繼續執行。例如:如果它開始打印並且紙張已用完,則它將掛起,直到打印機恢復打印,這是不正確的。
在此先感謝您的時間和耐心。
您必須提供對'BeginWrite'方法的回調,該方法將在寫入完成時調用。 – 2013-04-11 12:16:35
爲什麼在直接寫入基礎流時使用'BinaryWriter'?爲什麼不只是'f = new FileStream(...)',然後調用'f.BeginWrite'? – 2013-04-11 14:43:17
吉姆Mischel,我會得到什麼?我在方法一中做類似的程序。 outBuffer.BaseStream.BeginWrite – 2013-04-11 15:27:28