FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Using MsgRun along with TRY CATCH (Solved)
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Using MsgRun along with TRY CATCH (Solved)
Posted: Mon Jan 25, 2010 07:43 AM
Hi all,

My application connects to a remote database via Internet. I would like to give a message to the user saying that the application is trying to connect to the database via Internet, so that the user is aware what is happening in the background and thus avoid the feeling that the app is struck and hang while it is trying to connect to the database.

This is my code which connects to the the remote database via ADO
Code (fw): Select all Collapse
TRY
   oApp:oConnection:Open()
CATCH oError
   CursorArrow()
   MsgInfo("Failed to Connect to the Database available in the Internet."+CRLF+;
              "Please check whether internet is available or not in your PC","Connection failed")
   ShowSqlError(oError)
   RETURN .F.
END


I tried using MsgRun to show a message to the user while trying to connect. The code is given below
Code (fw): Select all Collapse
TRY
   MsgRun("Connecting to the server via Internet","Please wait",{ || oApp:oConnection:Open() } )
CATCH oError
   CursorArrow()
   MsgInfo("Failed to Connect to the Database available in the Internet."+CRLF+;
              "Please check whether internet is available or not in your PC","Connection failed")
   ShowSqlError(oError)
   RETURN .F.
END


If the Internet connection is OK and the connection to the database is successful everything works fine as expected. But if there is a problem with the Internet or database connection, my app will wait indefinitely displaying the wait message.

I need to use TRY CATCH to trap the error. I understand that MsgRun will wait until the code block command is executed, so this will effect TRY CATCH method

Is there any other method to overcome this problem ?

Thanks & Regards
Anser
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Using MsgRun along with TRY CATCH
Posted: Mon Jan 25, 2010 08:25 AM

TRY ..CATCH ..END should be within the codeblock that is executed by MsgRun.

Regards



G. N. Rao.

Hyderabad, India
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: Using MsgRun along with TRY CATCH
Posted: Mon Jan 25, 2010 08:45 AM

Dear Mr.Rao,

Thanks. You are right. The trick worked.

Regards
Anser

Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Using MsgRun along with TRY CATCH (Solved)
Posted: Mon Jan 25, 2010 01:12 PM

Please, could you put an example here with the complete solution ?

Thanks a lot in advance.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: Using MsgRun along with TRY CATCH (Solved)
Posted: Tue Jan 26, 2010 02:33 AM
Dear Mr.Michel,

This is what I have done.
Code (fw): Select all Collapse
...
lOk:=.F.
MsgRun("Trying to connect to the Database","Please wait..",;
             { || lOk:=OpenConnection() } )

if !lOk
   MsgInfo("Failed to Connect to the Database available in the Internet."+CRLF+;
              "Please check whether internet is available in your PC","Connection failed")
Endif
..
..
*-----------------------------------*
Function OpenConnection()
*-----------------------------------*
TRY
   oApp:oConnection:Open()
CATCH oError
   CursorArrow()
   ShowSqlError(oError)
   RETURN .F.
END
Return .T.


Regards
Anser
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Using MsgRun along with TRY CATCH (Solved)
Posted: Tue Jan 26, 2010 09:40 AM

Thank you very much.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Using MsgRun along with TRY CATCH (Solved)
Posted: Tue Jan 26, 2010 06:21 PM
Anser,

You may also find this function useful.

Regards,
James



Code (fw): Select all Collapse
/*
   Name       : IsInternet()  -> lConnected
   Description: Verify connection to Internet
   Syntaxis   : IsInternet(<cServer>)
   Date       : 9 March 2002
   Autor      : Jorge Mason Salinas
                htcsoft
                <!-- w --><a class="postlink" href="http://www.htcsoft.cl">www.htcsoft.cl</a><!-- w -->
*/

FUNCTION IsInternet(cServer) //Check Internet and any Server

  LOCAL oDummySock := TSocket():New(80) // Maybe any port, only for
WSAStartup()
  LOCAL lConnected

  DEFAULT cServer := "microsoft.com"

  lConnected = ( GetHostByName(cServer) <> "0.0.0.0" )

  oDummySock:End() // Only for WSACleanUp()

RETURN lConnected
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: Using MsgRun along with TRY CATCH (Solved)
Posted: Wed Jan 27, 2010 06:04 AM

Dear Mr.James,

Thank you for the information :)

Regards
Anser

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Using MsgRun along with TRY CATCH (Solved)
Posted: Wed Jan 27, 2010 07:21 AM

James, Anser,

FWH already provides IsInternet() --> lYesNo

regards, saludos

Antonio Linares
www.fivetechsoft.com

Continue the discussion