Today's interview of C#

本文深入探讨了C#中的异步编程技巧,包括async/await关键字的正确使用方式,如何定义可等待的任务,以及BeginInvoke和EndInvoke在回调机制中的作用。通过示例代码解释了理论与实践之间的差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I think they are advanced topics.

C#

  1. when will you use list, when will you use hashtable.

  2. when will you use Idispose.

  3.

 

Async and Await

 public async Task<string> DoAsync5()
        {
           Task<string> mytask=Task.Run<string>(() =>
            {
                for (int j = 0; j < 20; j++)
                {
                    System.Threading.Thread.Sleep(100);
                    Console.Write("------------Async thread: " + j.ToString() + "\r\n");
                }

                return "good";
            });

           return await mytask;

           //return "good";
            //Task.Factory.StartNew();

            //IAsyncResult ar = mydoasync2.BeginInvoke(new AsyncCallback(CallbackMethod), mydoasync2);

            //return result;

        }

 

 

publicasyncTask<string> DoAsync()

 

 

 

 

 

 

{

 

 

 

 

Task<string> mytask = DoAsync5();

 

for (int i = 0; i < 100; i++)

 

 

 

 

 

 

{

 

 

 

 

System.Threading.Thread.Sleep(10);

 

Console.Write("Main thread: " + i.ToString() + "\r\n");

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

string mytaskResult = await mytask;

 

Console.Write("Both threads: " + mytaskResult);

 

return mytaskResult;

 

 

 

 

 

 

}

 

 

 

 

 

If you want to use await and async, you must call await twice.

1. In async function's definition.

2. In the excution funciton.

You must await a awaitable task!!!!!!

How to make a awaitable task. Define a task by task.run and await it......

It is strangely.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("主线程测试开始..");
        AsyncMethod();
        Thread.Sleep(1000);
        Console.WriteLine("主线程测试结束..");
        Console.ReadLine();
    }
 
    static async void AsyncMethod()
    {
        Console.WriteLine("开始异步代码");
        var result = await MyMethod();
        Console.WriteLine("异步代码执行完毕");
    }
 
    static async Task<int> MyMethod()
    {
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("异步执行" + i.ToString() + "..");
            await Task.Delay(1000); //模拟耗时操作
        }
        return 0;
    }

 

 

------------------------------------------------------------------------------------------

About begininvoker and callback.

you need to send the delegation itself to callback. If not, callback can't end this invoker correctly. 

 

 

Now, there is theory and then there is practice. You have found, like many other developers before you, that you can often get away with ignoring this documented requirement. It may be an implementation detail whether EndInvoke actually does anything that's absolutely necessary to prevent your application from crashing, leaking memory, etc. But here's the thing: if it's a documented requirement, you really ought to do it. This is not just about theory; it's about protecting yourself in the event of change.

 

By documenting this requirement, the designers of this asynchronous calling mechanism basically gave themselves the freedom to change the way BeginInvoke and EndInvoke work down the line so that, if there were sufficient reason (e.g., a performance enhancement), EndInvoke could suddenly become a lot more necessary. Suppose it would suddenly result in a deadlock if you forgot it. They've already covered themselves by saying always call EndInvoke; if your app stops working because you didn't follow this requirement, the onus is on you.

 

I'm not saying this is necessarily a likely scenario. My point is simply that you shouldn't—or at least I wouldn't—ask "Is this really necessary?" with the mindset of If I can get away with leaving it out, then I will, since it is documented that you should do it.

http://stackoverflow.com/questions/4585042/is-delegate-endinvoke-really-necessary

 

转载于:https://www.cnblogs.com/gaoxianzhi/p/4426309.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值