Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
BindEvent() and Datasession
Message
 
To
All
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Title:
BindEvent() and Datasession
Miscellaneous
Thread ID:
00885592
Message ID:
00885592
Views:
53
OK, You might say "Yes sure, everyone knows that", but I didn't, and it took me a few hours to
realize what actually was going on here.

I have a project with an application-object that has an "Event"-object hooked to it.
The eventobject is the object other objects bind to, if they want to listen to certain
public events like "MailReceived". It only holds signatures (empty procedures with parameters)
and looks something like that
DEFINE class GloablEvents as line

  PROCEDURE MailReceived(tcMailID as String) as boolean
  ENDPROC

  PROCEDURE ...
  ENDPROC

ENDDEFINE
Then I have a handlerclass (handlerobject) that sits on a form with a private datasession that
is bound to the globalevent.object and listens. Nothing fancy, something like this:
DEFINE class MailListHandler as custom
  PROCEDURE INIT
     BindEvent(goApp.oEvents, "MailReceived", This "OnMailReceived")
  ENDPROC

  PROCEDURE OnMailReceived(tcMailID as string) as boolean
     local lcAlias, lcWhere, lnRecords
     
     lcAlias   = "SELECTION"
     lcWhere   = "where ID = '"+ tcMailID +"' "

     lnRecords = This.GetData(lcWhere, lcAlias)

     if lnRecords > 0 and used(lcAlias)

          ... then do some stuff

     endif
     return .T.
  ENDPROC

  PROCEDURE GetData(tcWhere as String, tcAlias as String) as Integer
      ...
      
      select <Fields> 
       from <table> 
        &tcWhere ;
         into cursor (tcAlias) 

      return _tally
  ENDPROC

ENDDEFINE
The problem was, the code at "... then do some stuff" NEVER got executed although
GetData() did a proper selection and returned 1.

The Problem was that the delegate method "OnMailReceived" always gets executed in the
GlobalEvent's datasession and GetData() gets executed in the datasession of the form.
Thus "used(lcAlias)" always returns .F. as the cursor resides in the form's datasession
while we are back in the globalEvents' session after the query has been executed.
I also was not able to manually change the datasession using "set datasession to" in
the delegate-method.

I have now moved everything to a third method that gets triggered by "OnMailReceived"
The working solution then looks like
DEFINE class MailListHandler as custom
  PROCEDURE INIT
     BindEvent(goApp.oEvents, "MailReceived", This "OnMailReceived")
  ENDPROC

  PROCEDURE OnMailReceived(tcMailID as string) as boolean
     Return This.ShowNewMail(tcMailID)
  ENDPROC

  PROCEDURE ShowNewMail(tcMailID as String) as Boolean
     local lcAlias, lcWhere, lnRecords
     lcAlias   = "SELECTION"
     lcWhere   = "where ID = '"+ tcMailID +"' "

     lnRecords = This.GetData(lcWhere, lcAlias)

     if lnRecords > 0 and used(lcAlias)

          ... then do some stuff

     endif

  ENDPROC

  PROCEDURE GetData(tcWhere as String, tcAlias as String) as Integer
      ...
  ENDPROC

ENDDEFINE
I thought this would be interesing as I was almost going up the wall
when I steped through the debugger and the cursor just vanished.
Regards from Berlin

Frank

Dietrich Datentechnik (Berlin)
Softwarekombinat Teltow (Teltow)

Frank.Dietrich@dd-tech.de
DFPUG # 327
Reply
Map
View

Click here to load this message in the networking platform