Click here to Skip to main content
15,888,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Experts,
Just let me know is the following code executed as asynchronously ?
My Code is shown below
C#
public class CompanyController : Controller
   {
public delegate void CompanyAuditRecordKeeper(CompanyModel company, string operationType);
    public ActionResult Create(CompanyModel oCompany)
        {
            //some code


            //I Used below code for asynchoronus call AddCompanyAuditRecords
           //some code
            CompanyAuditRecordKeeper auditkeeper = new CompanyAuditRecordKeeper(AddCompanyAuditRecords);
            auditkeeper.BeginInvoke(oCompany, "I", null, null);

            return Json("", JsonRequestBehavior.AllowGet);
        }
         public void AddCompanyAuditRecords(CompanyModel company, string operationType)
        {
         //  some code goes here

        }
  }


My Question is Will the 'AddCompanyAuditRecords' function be executed asynchronously?

Although I read it from MSDN that it will be executed asynchronously.From the following link
https://msdn.microsoft.com/en-us/library/2e08f6yc(v=vs.110).aspx

My confusion arises because an expert said me that it won't executed asynchronously please let me know?

What I have tried:

My Question is Will the 'AddCompanyAuditRecords' function be executed asynchronously?
Posted
Updated 12-Jul-16 20:30pm
v2

1 solution

The short answer is "maybe".
The official documentation says:
Executes the specified delegate asynchronously with the specified arguments
which is pretty clear!
But... It goes on to say:
on the thread that the control's underlying handle was created on.
Which means that it does get executed asynchronously, but only if the code it is executed on is not the UI thread.
So: "maybe".
Depends on exactly how that code is working on a wider context.
 
Share this answer
 
Comments
Anisuzzaman Sumon 13-Jul-16 2:20am    
In 'AddCompanyAuditRecords' function I just insert values in a table in mssql server
not more than that.If so then will you please tell me which thread is it executed?
OriginalGriff 13-Jul-16 3:55am    
We don't know.
It all depends on the rest of your code: if you call this method from a button click handler or similar then it's almost certainly on the UI thread, so Invoking will do nothing. So "no".
If your code already uses threading and the calling method is on a non-ui thread, then it's "yes".
But we don't have access to your code, so we can't tell.
Which leads us right back to the short answer: "maybe".
Anisuzzaman Sumon 13-Jul-16 4:03am    
Thanks

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