本文整理汇总了C#中State.Go方法的典型用法代码示例。如果您正苦于以下问题:C# State.Go方法的具体用法?C# State.Go怎么用?C# State.Go使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类State
的用法示例。
在下文中一共展示了State.Go方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Start
public Action Start(
Func<ArraySegment<byte>, Action, bool> next,
Action<Exception> error,
Action complete)
{
const int bufferSize = 4096;
var buffer = new byte[bufferSize];
var retval = new[] {0};
StreamAwaiter write = null;
long totalBytes = 0;
// This machine moves data from the spool to the replay storage
var state = new State();
state.Go = mark =>
{
try
{
switch (mark)
{
case 1:
goto mark1;
case 2:
goto mark2;
case 3:
goto mark3;
}
//// MEMORY LOOP
mark4:
// grab data from the spool
retval[0] = 0;
if (_spool.Pull(new ArraySegment<byte>(buffer, 0, bufferSize), retval, () => state.Go(3)))
return;
mark3:
if (retval[0] <= 0)
goto markreturn;
if (state.Cancelled)
goto markreturn;
// add to memory until threshold reached
_pages.Add(new ArraySegment<byte>(buffer, 0, retval[0]));
buffer = new byte[bufferSize]; // todo: get these from a page pool
totalBytes += retval[0];
if (totalBytes < _tempFileThresholdBytes)
goto mark4;
//// MEMORY LOOP
// create temp file at this point
_tempFileName = Path.GetTempFileName();
_tempFile = new FileStream(
_tempFileName,
FileMode.Create,
FileSystemRights.WriteData | FileSystemRights.ReadData,
FileShare.None,
4096,
FileOptions.SequentialScan | FileOptions.Asynchronous | FileOptions.WriteThrough | FileOptions.DeleteOnClose);
//// TEMPFILE LOOP
mark0:
// grab data from the spool
retval[0] = 0;
if (_spool.Pull(new ArraySegment<byte>(buffer, 0, bufferSize), retval, () => state.Go(1)))
return;
mark1:
// no more data - normal completion
if (retval[0] == 0)
goto markreturn;
// cancelled from outside the loop
if (state.Cancelled)
goto markreturn;
// send along to temp file
write = StreamAwaiter.Write(_tempFile, buffer, 0, retval[0]);
if (write.BeginAwait(() => state.Go(2)))
return;
mark2:
write.EndAwait();
// cancelled from outside the loop
if (state.Cancelled)
goto markreturn;
goto mark0;
//// TEMPFILE LOOP
markreturn:
_spoolComplete.Set();
}
catch (Exception ex)
{
/* need to comminucate failures outward!!! */
//.........这里部分代码省略.........
示例2: Replay
public Action Replay(Func<ArraySegment<byte>, Action, bool> next, Action<Exception> error, Action complete)
{
const int bufferSize = 4096;
var buffer = new byte[bufferSize];
StreamAwaiter read = null;
var pageEnumerator = _pages.GetEnumerator();
// This machine draws from the temp file and sends to the caller
var state = new State();
state.Go = mark =>
{
try
{
switch (mark)
{
case 1:
goto mark1;
case 2:
goto mark2;
case 4:
goto mark4;
}
//// MEMORY LOOP
mark5:
// move to the next memory page
if (!pageEnumerator.MoveNext())
goto mark3;
// send along to output
if (next(pageEnumerator.Current, () => state.Go(4)))
return;
mark4:
if (state.Cancelled)
goto markreturn;
goto mark5;
//// MEMORY LOOP
mark3:
if (_tempFile == null)
goto markreturn;
_tempFile.Seek(0, SeekOrigin.Begin);
//// TEMPFILE LOOP
mark0:
// read some data from the temp file
read = StreamAwaiter.Read(_tempFile, buffer, 0, bufferSize);
if (read.BeginAwait(() => state.Go(1)))
return;
mark1:
var count = read.EndAwait();
// end of file, normal completion
if (count == 0)
goto markreturn;
// cancelled from outside the loop
if (state.Cancelled)
return;
// send along to output
if (next(new ArraySegment<byte>(buffer, 0, count), () => state.Go(2)))
return;
mark2:
// cancelled from outside the loop
if (state.Cancelled)
return;
// loop for more
goto mark0;
//// TEMPFILE LOOP
markreturn:
complete();
}
catch (Exception ex)
{
error(ex);
}
};
// starts the machine only when the earlier transfer form spool to file is fully complete
_spoolComplete.Continue(() => state.Go(0));
return () => state.Cancelled = true;
}