c#debug中的异步方法 [英] Asynchronous method in c# debug

查看:200
本文介绍了c#debug中的异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在c#中使用begin invoke,end invoke实现了异步方法。



这里的场景是在main保存到DB之前,出现了一个名为log的方法保存到DB,这里我希望主要保存更快一点,因为客户端

将不会看到方法日志保存到DB。所以我让方法日志异步,假设log方法将在后台运行并且主保存方法也运行。



我的理解是正确的吗?我也想知道如何调试异步方法,因为调试就像同步函数一样,首先执行方法日志然后执行主保存。

i have implemented asynchronous method in c# using begin invoke,end invoke.

here the scenario is before main save to DB, there occurs a method called log which saves to DB ,Here i want main save to occur bit faster because , client
won't be seeing method log save to DB. so i made method log asynchronous assuming that log method will running in background and main save method also runs.

is my understanding correct here ? also i want to know how to debug asynchronous method, because debugging happens like synchronous function like method log executed first then main save gets executed.

推荐答案

不,这不正确,甚至没有关闭。



使用调用/ BeginInvoke ,您没有异步执行任何操作。在某种程度上,它恰恰相反:您将正在调用的方法与UI线程同步,并且在 BeginInvoke 的情况下,也与调用线程同步。我在这里解释了:

Control.Invoke() vs. Control.BeginInvoke() [ ^ ],

Treeview扫描仪和MD5的问题 [ ^ ]。



这没有什么不妥,但只有当你做某事时才需要一个单独的线程(除了UI线程)并希望通知UI并根据其他线程数据执行一些UI操作并由该线程触发。它与快一点无关,但是,如果没有委托机制,你根本无法更新UI线程。



在你的问题中,没有迹象表明你正在使用任何其他线程,所以你所做的一切都是毫无意义的。如果您的代码有效,那只是因为 Invoke / BeginInvoke 方法即使您在UI线程中使用它们也可以工作。这种万无一失的后备功能。 :-)



-SA
No, it's not correct, not even close.

Using Invoke/BeginInvoke, you are not doing anything asynchronously. In a way, it's just the opposite: you synchronize the method being invoking with the UI thread, and, in case of BeginInvoke, also with the calling thread. I explained it here:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

There is nothing wrong with that, but this is only needed if you do something is a separate thread (other than the UI thread) and want notify the UI and perform some UI action based on the other thread data and triggered by that thread. It has nothing to do with "bit faster", but, without the delegation mechanism, you would not be able to update the UI thread at all.

In your question, there is no indication that you are using any other thread, so all you do is pointless. If your code works, it's only because Invoke/BeginInvoke methods are designed to work even if you use them in a UI thread. Such a fool-proof "fallback" feature. :-)

—SA


这篇关于c#debug中的异步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆