0

I was attempting to write a few asynchronous functions for MySQL but the documentation is so poor and I couldn't find any examples. Could anyone explain the function or give an example of it:

public void GetData(string commandText, Action<IAsyncResult> mySqlCallback)
{
    MySqlCommand command = mySqlConnection.CreateCommand();
    command.CommandText = commandText;

    IAsyncResult asyncResult = command.BeginExecuteReader();

    //return command.EndExecuteReader(asyncResult);
}

public void Execute(string commandText)
{
    MySqlCommand command = mySqlConnection.CreateCommand();
    command.CommandText = commandText;

    IAsyncResult asyncResult = command.BeginExecuteReader(0);

    command.EndExecuteReader(asyncResult);
}
Alexandre Neukirchen
  • 2,713
  • 7
  • 26
  • 36
  • The `SqlCommand.BeginExecuteReader()` should operate identically, have a look at the MSDN entry for it: https://msdn.microsoft.com/en-us/library/1a674khd%28v=vs.110%29.aspx – gmiley Dec 24 '15 at 18:03
  • You should use the ExecuteReaderAsync function or convert it to a Task – Sievajet Dec 24 '15 at 18:23
  • Could you give me post an answer which gives an example of it. Thanks. – Avid Programmer Dec 24 '15 at 18:32
  • Can anybody help with this? – Avid Programmer Dec 24 '15 at 18:49
  • Seriously will this topic die? – Avid Programmer Dec 24 '15 at 20:51
  • Welcome on Stackoverflow where no body will ever answer your question past the 5 minutes mark ! Task class mean you have to understand a basic of [asyncronous programming](https://msdn.microsoft.com/en-us/library/hh191443.aspx) As for the sample you could probably use the [ADO.NET exemple](http://blogs.msdn.com/b/adonet/archive/2012/07/15/using-sqldatareader-s-new-async-methods-in-net-4-5-beta-part-2-examples.aspx) (with a few changes). Basicly your GetData() will look like async Task GetData(...) { .... return await command.ExecuteReaderAsync(); } – 0xCDCDCDCD Jan 24 '16 at 14:52
  • Starting using async mean all your methods will use it, if you [can't you will have to schedule the task](http://stackoverflow.com/questions/9208921/async-on-main-method-of-console-app). Be aware that if you have a UI you may [create a dead lock](http://blogs.msdn.com/b/pfxteam/archive/2011/01/13/10115163.aspx) (aka stop the code from running anymore) if you to try to wait on GetData() – 0xCDCDCDCD Jan 24 '16 at 14:54

0 Answers0