Async is asynchronous, but its function is to call the Asynchronous Method activity synchronously. Asynccodeactivity inherits activity as an abstract class. Override uses the [cachemetadata method] and provides two Abstract METHODS: [beginexecute] and [endexecute]. The main purpose of asynccodeactivity is to implement the question of how to wait for an asynchronous operation in the activity to run down, that is, how to turn parallel into serial.
Example given by Microsoft:
static void Main() { FileWriter writer = new FileWriter(); WorkflowInvoker.Invoke(writer); Console.WriteLine("Hit <enter> to exit..."); Console.ReadLine(); }
Filewriter :::::::::::::::::::::::::::::public sealed class FileWriter : AsyncCodeActivity { public FileWriter() : base() { } 1 protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state) { string tempFileName = Path.GetTempFileName(); Console.WriteLine("Writing to file: " + tempFileName); FileStream file = File.Open(tempFileName, FileMode.Create); context.UserState = file; byte[] bytes = UnicodeEncoding.Unicode.GetBytes("123456789"); return file.BeginWrite(bytes, 0, bytes.Length, callback, state); } 2 protected override void EndExecute(AsyncCodeActivityContext context, IAsyncResult result) { System.Threading.Thread.Sleep(2000);
FileStream file = (FileStream)context.UserState; try { file.EndWrite(result); file.Flush(); } finally { file.Close(); } } }
Result:
Writing to file: C:\Documents and Settings\Administrator\Local Settings\Temp\tmp
A4.tmp
(2 seconds later)
Hit <enter> to exit...
In addition:
Stream. beginwrite method beginwrite (
Byte [] buffer,
Int offset,
Int count,
Asynccallback callback,
Object state
)
Parameters
-
Buffer
-
Type: system. byte []
The buffer from which data is written.
-
Offset
-
Type: system. int32
The Byte offset in the buffer.
-
Count
-
Type: system. int32
Maximum number of bytes written.
-
Callback
-
Type: system. asynccallback
Optional asynchronous callback, called when writing is completed.
-
State
-
Type: system. Object
An object provided by a user that distinguishes the specified asynchronous write request from other requests.
Return Value Type: system. iasyncresult
Iasyncresult of asynchronous write (may still be suspended ).
The default Implementation of beginwrite in the stream will synchronously call the write method, which means that write may be blocked in some streams. However
For some classes (such as filestream and networkstream), these instances fully support asynchronous operations. Therefore, the call to beginwrite will not
Blocking on those streams. You can override beginwrite (for example, use Asynchronous delegation) to provide asynchronous behavior.
Pass the iasyncresult returned by the current method to endwrite to ensure that the write operation is complete and resources are released accordingly. Required for each beginwrite call
Call endwrite once. This can be done by calling the same code as beginwrite, or in the callback passed to beginwrite. If
If an error occurs during step writing, no exception is thrown before the iasyncresult returned by this method calls endwrite.