D L

D L

  • NA
  • 11
  • 17k

C# BeginInvoke and Callback

Oct 15 2013 3:11 PM

I am having a very hard time getting this to work like I want it to.

2 Classes: Form1 and Database. Database class purpose - all DB connectivity, updates, everything. (trying to be a good developer and not throw everything in one giant class)

Database class has a method for checking DB connectivity. Form1 has a method for updating the GUI based on DB connectivity (using bool parameter).

Database connectivity check method can cause the GUI to lag, so I am using BeginInvoke to call it. When it is done checking I want to callback to the method on Form1 that updates the GUI. Creating an object for the database class (no problem). Aynchronously calling a method on that object to check DB connectivity using BeginInvoke (no problem). Getting it to call back my method in Form1 class with a bool value to update the GUI (problem). I have created a delegate with the same signature as my update GUI method. I have tried to pass it in using BeginInvoke with a callback, but I don't guess it works that way. I've created an event to fire from the DB checking method to call the GUI updating method and it works, but it runs on the thread created by BeginInvoke and therefore complains.

How can I:

class Form1

{

       Form1()

       {

         Database db = new Database();

         // call db's method CheckDBConnectivity using a delegate and begin invoke - also pass in a callback to Form1's GUIUpdateMethod with bool parameter

        }




 

   GUIUpdateMethod(bool status)

   {

   //update GUI based on true/false

   }

}





 

class Database

{

   Database()

   {

   

   // blah blah blah

   }

   CheckDBConnectivity()

   {

    // call back Form1's UpdateGUI method with a bool value

   }

}


Any help is greatly appreciated as I am about to run around yelling like a banshee and foaming at the mouth. :-)




 






 






 




 



 





 




 



 




 




 




 






 



 




 


 


Answers (1)