本文整理汇总了C#中FileStream.BeginWrite方法的典型用法代码示例。如果您正苦于以下问题:C# FileStream.BeginWrite方法的具体用法?C# FileStream.BeginWrite怎么用?C# FileStream.BeginWrite使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FileStream
的用法示例。
在下文中一共展示了FileStream.BeginWrite方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Save
public int Save(string filePath, byte[] fileData)
{
using (FileStream stream = new FileStream (filePath, FileMode.OpenOrCreate)) {
stream.BeginWrite(fileData, 0, fileData.Length, new System.AsyncCallback(OnSave), new FageFileState(++_countId, filePath, stream, fileData));
}
return _countId;
}
示例2: WorkItemMethod
public static void WorkItemMethod(object mainEvent)
{
Console.WriteLine("\nStarting WorkItem.\n");
AutoResetEvent autoEvent = new AutoResetEvent(false);
// Create some data.
const int ArraySize = 10000;
const int BufferSize = 1000;
byte[] byteArray = new Byte[ArraySize];
new Random().NextBytes(byteArray);
// Create two files and two State objects.
FileStream fileWriter1 =
new FileStream(@"C:\[email protected]##.dat", FileMode.Create,
FileAccess.ReadWrite, FileShare.ReadWrite,
BufferSize, true);
FileStream fileWriter2 =
new FileStream(@"C:\[email protected]##.dat", FileMode.Create,
FileAccess.ReadWrite, FileShare.ReadWrite,
BufferSize, true);
State stateInfo1 = new State(fileWriter1, autoEvent);
State stateInfo2 = new State(fileWriter2, autoEvent);
// Asynchronously write to the files.
fileWriter1.BeginWrite(byteArray, 0, byteArray.Length,
new AsyncCallback(EndWriteCallback), stateInfo1);
fileWriter2.BeginWrite(byteArray, 0, byteArray.Length,
new AsyncCallback(EndWriteCallback), stateInfo2);
// Wait for the callbacks to signal.
autoEvent.WaitOne();
autoEvent.WaitOne();
fileWriter1.Close();
fileWriter2.Close();
Console.WriteLine("\nEnding WorkItem.\n");
// Signal Main that the work item is finished.
((AutoResetEvent)mainEvent).Set();
}
示例3: runTest
//.........这里部分代码省略.........
{
fs2.Write(new Byte[]{1}, 0, 1);
iCountErrors++;
printerr( "Error_2000m! Expected exception not thrown");
}
catch (ObjectDisposedException iexc)
{
printinfo( "Info_2000n! Caught expected exception, iexc=="+iexc.Message);
}
catch (Exception exc)
{
iCountErrors++;
printerr( "Error_2000o! Incorrect exception thrown, exc=="+exc.ToString());
}
iCountTestcases++;
try
{
fs2.Read(new Byte[1], 0, 1);
iCountErrors++;
printerr( "Error_2000p! Expected exception not thrown");
}
catch (ObjectDisposedException iexc)
{
printinfo( "Info_2000q! Caught expected exception, iexc=="+iexc.Message);
}
catch (Exception exc)
{
iCountErrors++;
printerr( "Error_2000r! Incorrect exception thrown, exc=="+exc.ToString());
}
iCountTestcases++;
try
{
fs2.EndWrite(fs2.BeginWrite(new Byte[]{1}, 0, 1, null, null));
iCountErrors++;
printerr( "Error_2000s! Expected exception not thrown");
}
catch (ObjectDisposedException iexc)
{
printinfo( "Info_2000t! Caught expected exception, iexc=="+iexc.Message);
}
catch (Exception exc)
{
iCountErrors++;
printerr("Error_2000u! Incorrect exception thrown, exc=="+exc.ToString());
}
iCountTestcases++;
try
{
fs2.EndRead(fs2.BeginRead(new Byte[1], 0, 1, null, null));
iCountErrors++;
printerr("Error_2000v! Expected exception not thrown");
}
catch (ObjectDisposedException iexc)
{
printinfo( "Info_2000w! Caught expected exception, iexc=="+iexc.Message);
}
catch( Exception exc)
{
iCountErrors++;
printerr( "Error_2000x! Incorrect exception thrown, exc=="+exc.ToString());
}
iCountTestcases++;
try
{
fs2.Flush();
示例4: SaveImage
private IEnumerator SaveImage(string path, byte[] bytes, Action saveComplete = null)
{
var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, 4096, false);
fs.BeginWrite(bytes, 0, bytes.Length, new AsyncCallback (EndWriteCallback), fs);
yield return File.Exists (path);
if (saveComplete != null) {
saveComplete();
}
}
示例5: runTest
//.........这里部分代码省略.........
{
iCountErrors++;
printerr( "Error_47f7v_"+i+"! Expected=="+i+" , got=="+b);
}
i++;
}
fs2.Close();
fs3.Close();
strLoc = "Loc_487ty";
iCountTestcases++;
try
{
fs2 = new FileStream(new IntPtr(2), (FileAccess)(-2), true, 2000);
iCountErrors++;
printerr( "Error_f489y! Expected exception not thrown");
}
catch (ArgumentException aexc)
{
printinfo( "Info_4t98c! Caught expected exception, aexc=="+aexc.Message);
}
catch (Exception exc)
{
iCountErrors++;
printerr( "Error_4398g! Incorrect exception thrown, exc=="+exc.ToString());
}
strLoc = "Loc_399f9";
fs2 = new FileStream(filName, FileMode.Create);
fs3 = new FileStream(fs2.Handle, FileAccess.Read, true, 4000);
fs3.Close();
iCountTestcases++;
try
{
fs2.Write(new Byte[]{1}, 0, 1);
fs2.Flush();
iCountErrors++;
printerr( "Error_3f8vc! Expected exception not thrown");
}
catch (IOException iexc)
{
printinfo( "Info_398fc! Caught expected exception, iexc=="+iexc.Message);
}
catch (Exception exc)
{
iCountErrors++;
printerr( "Error_98gyg! Incorrect exception thrown, exc=="+exc.ToString());
}
fs2.Close();
strLoc = "Loc_99f99";
fs2 = new FileStream(filName, FileMode.Create);
fs3 = new FileStream(fs2.Handle, FileAccess.Read, false, 16000);
fs3.Close();
try
{
fs2.Write(new Byte[]{1}, 0, 1);
fs2.Flush();
}
catch (Exception exc)
{
iCountErrors++;
printerr( "Error_3989c! Unexpected exception, exc=="+exc.ToString());
}
fs2.Close();
strLoc = "Loc_498vy";
fs2 = new FileStream("Bug.txt", FileMode.Create);
try
{
fs3 = new FileStream(fs2.Handle, FileAccess.ReadWrite, false, Int32.MaxValue/100);
Byte[] bArr = new Byte[100*10];
for(int ii = 0 ; ii < bArr.Length ;ii++)
bArr[i] = (Byte)ii;
IAsyncResult iar = fs3.BeginWrite(bArr, 0, bArr.Length, null, null);
fs3.EndWrite(iar);
fs3.Close();
fs2.Close();
}
catch( Exception e )
{
Console.WriteLine("Unexpected exception occured... " + e.ToString() );
}
File.Delete("Bug.txt");
if(File.Exists(filName))
File.Delete(filName);
}
catch (Exception exc_general )
{
++iCountErrors;
Console.WriteLine (s_strTFAbbrev + " : Error Err_8888yyy! strLoc=="+ strLoc +", exc_general=="+exc_general.ToString());
}
if ( iCountErrors == 0 )
{
Console.WriteLine( "paSs. "+s_strTFName+" ,iCountTestcases=="+iCountTestcases.ToString());
return true;
}
else
{
Console.WriteLine("FAiL! "+s_strTFName+" ,iCountErrors=="+iCountErrors.ToString()+" , BugNums?: "+s_strActiveBugNums );
return false;
}
}
示例6: runTest
//.........这里部分代码省略.........
iCountErrors++;
printerr( "Error_t958g! Incorrect exception thrown, exc=="+exc.ToString());
}
fs2.Close();
}
strLoc = "Loc_399il";
for(int iLoop = 0 ; iLoop < iArrLargeValues.Length ; iLoop++)
{
fs2 = new FileStream(fileName, FileMode.Open);
iCountTestcases++;
bArr = new Byte[]{0,1};
try
{
fs2.BeginRead(bArr, 0, iArrLargeValues[iLoop], null, null);
iCountErrors++;
printerr( "Error_5520j! Expected exception not thrown");
}
catch (ArgumentException aexc)
{
printinfo( "Info_88tum! Caught expected exception, aexc=="+aexc.Message);
}
catch (Exception exc)
{
iCountErrors++;
printerr( "Error_2090t! Incorrect exception thrown, exc=="+exc.ToString());
}
fs2.Close();
}
strLoc = "Loc_388hj";
fs2 = new FileStream(fileName, FileMode.Open);
ascb = new AsyncCallback(this.CallBackMethod);
bCallBackCalled = false;
bArr = new Byte[]{0,1,2,3};
iar = fs2.BeginWrite(bArr, 0, 4, ascb, 5);
fs2.EndWrite(iar);
Thread.Sleep(1000);
iCountTestcases++;
if(!bCallBackCalled)
{
iCountErrors++;
printerr( "Error_489xh! CallBackmethod not called");
}
iCountTestcases++;
if(!iar.IsCompleted)
{
iCountErrors++;
printerr( "Error_29ycy! Completed not set correctly");
}
iCountTestcases++;
if(!iar.CompletedSynchronously)
{
iCountErrors++;
printerr( "Error_8998c! Not done async");
}
iCountTestcases++;
if((Int32)iar.AsyncState != 5)
{
iCountErrors++;
printerr( "Error_20hvb! Incorrect AsyncState");
}
fs2.Position = 0;
bResultArr = new Byte[8];
iar = fs2.BeginRead(bResultArr, 2, 3, ascb, 5);
fs2.EndRead(iar);
for(int i = 2 ; i < 5 ; i++)
{
示例7: runTest
public bool runTest()
{
Console.WriteLine(s_strTFPath + "\\" + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer);
String strLoc = "Loc_000oo";
String strValue = String.Empty;
int iCountErrors = 0;
int iCountTestcases = 0;
try
{
String filName = s_strTFAbbrev+"TestFile";
Stream fs2;
IAsyncResult iar;
Byte[] bArr;
if(File.Exists(filName))
File.Delete(filName);
strLoc = "Loc_100aa";
fs2 = new FileStream(filName, FileMode.Create);
iCountTestcases++;
try
{
fs2.EndWrite(null);
iCountErrors++;
printerr( "Error_100bb! Expected exception not thrown");
}
catch (ArgumentNullException aexc)
{
printinfo( "Info_100cc! Caught expected exception, aexc=="+aexc.Message);
}
catch (Exception exc)
{
iCountErrors++;
printerr( "Error_100dd Incorrect exception thrown, exc=="+exc.ToString());
}
fs2.Close();
strLoc = "Loc_200aa";
fs2 = new FileStream(filName, FileMode.Create);
iar = fs2.BeginRead(new Byte[0], 0, 0, null, null);
iCountTestcases++;
try
{
fs2.EndWrite(iar);
iCountErrors++;
printerr( "Error_200bb! Expected exception not thrown");
}
catch (ArgumentException aexc)
{
printinfo ("Info_200cc! Caught expected exception, aexc=="+aexc.Message);
}
catch (Exception exc)
{
iCountErrors++;
printerr( "Error_200dd! Incorrect exception thrown, exc=="+exc.ToString());
}
fs2.Close();
strLoc = "Loc_300aa";
fs2 = new FileStream(filName, FileMode.Create);
bArr = new Byte[1024*1000];
for(int i = 0 ; i < bArr.Length ; i++)
bArr[i] = (Byte)i;
iar = fs2.BeginWrite(bArr, 0, bArr.Length, null, null);
fs2.EndWrite(iar);
iCountTestcases++;
if(!iar.IsCompleted)
{
iCountErrors++;
printerr( " Error_300dd! Operation should be complete");
}
iCountTestcases++;
iCountTestcases++;
if(fs2.Length != bArr.Length)
{
iCountErrors++;
printerr( "Error_300bb! Expected=="+bArr.Length+", Return=="+fs2.Length);
}
fs2.Close();
strLoc = "Loc_400aa";
fs2 = new FileStream(filName, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 100, true);
bArr = new Byte[1000*1024];
for(int i = 0 ; i < bArr.Length ; i++)
bArr[i] = (Byte)i;
iar = fs2.BeginWrite(bArr, 0, bArr.Length, null, null);
fs2.EndWrite(iar);
iCountTestcases++;
if(!iar.IsCompleted)
{
iCountErrors++;
printerr( "Error_400bb! Operation should be complete");
}
iCountTestcases++;
iCountTestcases++;
if(fs2.Length != bArr.Length)
{
iCountErrors++;
printerr( "Error_400dd! Expected=="+bArr.Length+", Return=="+fs2.Length);
}
fs2.Close();
if(File.Exists(filName))
File.Delete(filName);
}
catch (Exception exc_general )
//.........这里部分代码省略.........
示例8: DownloadCoroutine
private IEnumerator DownloadCoroutine()
{
WWW www = null;
if (cached)
www = new WWW("file://" + Application.temporaryCachePath + "/" + this.guid + ".png");
else
www = new WWW(url);
#if DEBUG_LOG
Debug.Log("DEBUG: TileEntry.DownloadCoroutine: (down)loading from tile url: " + www.url);
#endif
yield return www;
if (www.error == null && www.text.Contains("404 Not Found") == false)
{
if (www.texture.isBogus())
{
#if DEBUG_LOG
Debug.LogError("DEBUG: TileEntry.DownloadCoroutine: image from cache is bogus, trying to download it: " + www.url + " [" + url + "]");
#endif
//TileDownloader.Instance.DeleteCachedTile(this);
//TileDownloader.Instance.Get(url, material);
error = true;
}
else
{
material.mainTexture = www.texture;
material.mainTexture.wrapMode = TextureWrapMode.Clamp;
material.mainTexture.filterMode = FilterMode.Trilinear;
if (this.cached == false)
{
// write the png asynchroneously
byte[] bytes = (material.mainTexture as Texture2D).EncodeToPNG();
this.size = bytes.Length;
this.timestamp = (DateTime.Now - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
this.guid = Guid.NewGuid().ToString();
FileStream fs = new FileStream(Application.temporaryCachePath + "/" + this.guid + ".png", FileMode.Create);
fs.BeginWrite(bytes, 0, bytes.Length, new AsyncCallback(EndWriteCallback), this);
#if DEBUG_LOG
Debug.Log("DEBUG: TileEntry.DownloadCoroutine: done downloading: " + www.url + ", writing to cache: " + fs.Name);
#endif
}
else
{
#if DEBUG_LOG
Debug.Log("DEBUG: TileEntry.DownloadCoroutine: done loading from cache: " + www.url + " [" + url + "]");
#endif
}
}
}
else
{
error = true;
#if DEBUG_LOG
Debug.LogError("ERROR: TileEntry.DownloadCoroutine: done downloading: " + www.url + " with error: " + www.error + " (" + www.text + ")");
#endif
}
}
示例9: DownloadCoroutine
private IEnumerator DownloadCoroutine()
{
WWW www = null;
string ext = Path.GetExtension(url);
if (ext.Contains("?"))
ext = ext.Substring(0, ext.IndexOf('?'));
#if !UNITY_WEBPLAYER
if (cached && File.Exists(Application.temporaryCachePath + "/" + this.guid + ext))
{
www = new WWW("file:///" + Application.temporaryCachePath + "/" + this.guid + ext);
#if DEBUG_LOG
Debug.Log("DEBUG: TileDownloader.DownloadCoroutine: loading tile from cache: url: " + www.url);
#endif
}
else
#endif
{
www = new WWW(url);
#if DEBUG_LOG
Debug.Log("DEBUG: TileDownloader.DownloadCoroutine: loading tile from provider: url: " + www.url
#if !UNITY_WEBPLAYER
+ "(cached: " + cached + ")"
#endif
);
#endif
}
yield return www;
#if DEBUG_PROFILE
UnitySlippyMap.Profiler.Begin("TileDownloader.TileEntry.DownloadCoroutine");
#endif
#if DEBUG_PROFILE
UnitySlippyMap.Profiler.Begin("www error test");
#endif
if (www.error == null && www.text.Contains("404 Not Found") == false)
{
#if DEBUG_PROFILE
UnitySlippyMap.Profiler.End("www error test");
#endif
#if DEBUG_PROFILE
UnitySlippyMap.Profiler.Begin("www.texture");
#endif
Texture2D texture = new Texture2D(1, 1, TextureFormat.ARGB32, true);
www.LoadImageIntoTexture(texture);
#if DEBUG_PROFILE
UnitySlippyMap.Profiler.End("www.texture");
#endif
#if DEBUG_PROFILE
UnitySlippyMap.Profiler.Begin("is cached?");
#endif
#if !UNITY_WEBPLAYER
if (this.cached == false)
{
#if DEBUG_PROFILE
UnitySlippyMap.Profiler.End("is cached?");
#endif
#if DEBUG_PROFILE
UnitySlippyMap.Profiler.Begin("set TileEntry members");
#endif
byte[] bytes = www.bytes;
this.size = bytes.Length;
this.guid = Guid.NewGuid().ToString();
#if DEBUG_PROFILE
UnitySlippyMap.Profiler.End("set TileEntry members");
#endif
#if DEBUG_PROFILE
UnitySlippyMap.Profiler.Begin("new FileStream & FileStream.BeginWrite");
#endif
FileStream fs = new FileStream(Application.temporaryCachePath + "/" + this.guid + ext, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true);
fs.BeginWrite(bytes, 0, bytes.Length, new AsyncCallback(EndWriteCallback), new AsyncInfo(this, fs));
#if DEBUG_PROFILE
UnitySlippyMap.Profiler.End("new FileStream & FileStream.BeginWrite");
#endif
#if DEBUG_LOG
Debug.Log("DEBUG: TileEntry.DownloadCoroutine: done loading: " + www.url + ", writing to cache: " + fs.Name);
#endif
}
else
{
#if DEBUG_PROFILE
UnitySlippyMap.Profiler.End("is cached?");
#endif
#if DEBUG_LOG
Debug.Log("DEBUG: TileEntry.DownloadCoroutine: done loading from cache: " + www.url + " [" + url + "]");
#endif
}
this.timestamp = (DateTime.Now - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
#endif
//.........这里部分代码省略.........