Click here to Skip to main content
15,915,160 members

Comments by Member 0123456789 (Top 12 by date)

Member 0123456789 9-Jan-16 6:48am View    
Deleted
The function StartButton_Click is async event handler on some kind of GUI interface and instead of closing any callbacks, it calls another functions (WorkerMethod and PostMessage). The function WorkerMethod is async too and basically wait (simulate some kind of data manipulations), check if there isn't timeout and returns a string. Then the StartButton_Click calls the second function PostMessage, which takes the string and append a property of I guess the GUI interface.
The timeout with the CancellationToken can be useful if you want to timeout the asynchronous function. But with this architecture you will have to write one specific asynchronous containing function with additional two or more specific functions instead of one specific callback closing function and one or more callbacks.
Member 0123456789 9-Jan-16 5:55am View    
I declared and assigned three Task objects for each printName calls and at the end of Main added:
Task.WaitAll(task9000, task6000, task3000);
/* if I need to wait any of the containing callback function to be ready I can just add the below in a synchronous block
task9000.Wait();
or/and
task6000.Wait();
or/and
task3000.Wait();
*/
Main cannot be asynchronous and there is no point of third closing function which will need to be waited anyway.
Member 0123456789 8-Jan-16 16:15pm View    
You're completely right - there will be something else - like piping streams or some kind of manipulating data. With the above code I get error while compiling the code in the console - "Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.", while with the code in Solution 1 the compiler doesn't catch it. Both works.
Member 0123456789 8-Jan-16 16:07pm View    
This works perfectly - although probably is really wrong architecture for C#:

public static async Task hello9000(string name, string message)
{
await Task.Run(delegate()
{
Stopwatch timer = new Stopwatch();
timer.Start();
while (timer.ElapsedMilliseconds < 9000)
{
//wait, wait and wait...
}
timer.Stop();
//Console.WriteLine(name);
Console.WriteLine(message);
});
}

public static async Task hello6000(string name, string message)
{
await Task.Run(delegate()
{
Stopwatch timer = new Stopwatch();
timer.Start();
while (timer.ElapsedMilliseconds < 6000)
{
//wait, wait and wait...
}
timer.Stop();
//Console.WriteLine(name);
Console.WriteLine(message);
});
}

public static async Task hello3000(string name, string message)
{
await Task.Run(delegate()
{
Stopwatch timer = new Stopwatch();
timer.Start();
while (timer.ElapsedMilliseconds < 3000)
{
//wait, wait and wait...
}
timer.Stop();
//Console.WriteLine(name);
Console.WriteLine(message);
});
}

public static async Task printName(string name, string message, Func<string, string, Task> callback)
{
Console.WriteLine(name + " started on thread " + System.Threading.Thread.CurrentThread.ManagedThreadId);
await callback(name, message);
}


printName("hello9000", "hello9000 ended on thread " + System.Threading.Thread.CurrentThread.ManagedThreadId, hello9000);
printName("hello6000", "hello6000 ended on thread " + System.Threading.Thread.CurrentThread.ManagedThreadId, hello6000);
printName("hello3000", "hello3000 ended on thread " + System.Threading.Thread.CurrentThread.ManagedThreadId, hello3000);
Member 0123456789 8-Jan-16 15:53pm View    
The function HelloDelay is the closing function for the callback. The function Run is container of Tasks for the callbacks, which are Console.WriteLine. This will only output the messages asynchronously, while I intended a custom callback functions (declared or anonymous), which I can pass separately on a model of callback containing function, so I can call the last asynchronously.

function container(string message, callback)
{
/* do something */
callback(message);//when the above finish call me through function pointer
}
function callback(string message)
{
/* do something */
}

/* do something */
container("hello", callback);//start this without blocking the code
/* do something */

/* ...somewhere in the time and space container function finish... */

Simple as it is, not yet simple for most languages, not build this way.