Click here to Skip to main content
15,896,359 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted

1 solution

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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900