# mco_uda_async_event_wait
RT

> Block the thread until the event is fired .

### Prototype

        MCO_RET mco_uda_async_event_wait(	/*IN*/ mco_db_h db,
                           /*IN*/ unsigned short struct_no,
                           /*IN*/ unsigned short event_no );

### Arguments

db | The database handle
struct_no | The structure/class number (must be between 0 and `mco_dict_struct_count()` - 1)
event_no | The event number

### Description

> This function blocks the thread until the specified event is fired by the application, or the application explicitly releases the event handler by calling function `mco_uda_async_event_release()` or `mco_uda_async_event_release_all()`.

### Return Codes

MCO_S_OK | The event handler was released successfully
MCO_E_INVALID_HANDLE | The database handle is invalid
MCO_S_EVENT_RELEASED | The event was released by the application

### Example

        Application snippet:

        typedef struct ThrParam_
        {
            sample_task_t task;
            mco_db_h db;
            unsigned short struct_no;
            unsigned short event_no;
            int event_count;
            int finished;
        } ThrParam;

        void async_event_handler( sample_task_t * descriptor )
        {
            ThrParam* thr_p = (ThrParam*) descriptor->param;
            mco_dict_struct_info_t struct_info;
            mco_dict_field_info_t field_info;
            mco_dict_event_info_t event_info;
            mco_db_h db = thr_p->db;
            char *event_type;
            MCO_RET rc;

            thr_p->finished = 0;
            thr_p->event_count = 0;

            mco_dict_struct(metadict, 0, thr_p->struct_no, &struct_info);
            mco_dict_event(metadict, 0, thr_p->struct_no, thr_p->event_no, &event_info);
            if (event_info.type == MCO_EVENT_UPDATE) {
                mco_dict_field(metadict, 0, thr_p->struct_no, event_info.field_no, &field_info);
            }
            switch (event_info.type) {
                case MCO_EVENT_UPDATE       : event_type = "UPDATE"; break;
                case MCO_EVENT_NEW          : event_type = "NEW"; break;
                case MCO_EVENT_DELETE       : event_type = "DELETE"; break;
                case MCO_EVENT_DELETE_ALL   : event_type = "DELETE_ALL"; break;
                case MCO_EVENT_CHECKPOINT   : event_type = "CHECKPOINT"; break;
                case MCO_EVENT_CLASS_UPDATE : event_type = "CLASSUPDATE"; break;
            }
            for (;;)
            {
                rc = mco_uda_async_event_wait(db, thr_p->struct_no, thr_p->event_no);

                printf("Async. event : class %s, event_no=%d, type=%s ", struct_info.name, thr_p->event_no, event_type);
                if (event_info.type == MCO_EVENT_UPDATE) {
                    printf("field=%s ", field_info.name);
                }
                if (rc != MCO_S_OK) {
                    printf(" exiting. Num. of events=%d\n", thr_p->event_count);
                    thr_p->finished = 1;
                    return ;
                }
                ++thr_p->event_count;
                printf("\n");
            }
        }

        void start_async_handlers(mco_db_h db)
        {
            unsigned short struct_count, s, e;
            mco_dict_struct_info_t struct_info;
            unsigned short thr_num = 0;

            mco_dict_struct_count(metadict, 0, &struct_count);
            for (s = 0; s < struct_count; ++s) {
                mco_dict_struct(metadict, 0, s, &struct_info);
                for (e = 0; e < struct_info.event_count; ++e) {
                    all_thr_param[thr_num].db = db;
                    all_thr_param[thr_num].struct_no = s;
                    all_thr_param[thr_num].event_no  = e;
                    sample_start_connected_task(&all_thr_param[thr_num].task, async_event_handler, db_name, &all_thr_param[thr_num]);
                    ++thr_num;
                    sample_sleep(100);
                }
            }
        }

        int main(int argc, char* argv[])
        {
            MCO_RET rc;
            mco_db_h db;
            ...
            start_async_handlers(db);
            ...
        }

### Files

Header file:
mcouda.h
Source file:
mcouda.c
Library:
libmcouda.a