Synchronous events? (c++/cli)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BahatiSiD
    New Member
    • Oct 2008
    • 9

    Synchronous events? (c++/cli)

    Hello!

    I have a class instance that fires events and they are handled in a form.

    I believe this kind of event is called trivial:
    Code:
    public: delegate void CpuStepEventHandler(Object^ sender, CpuStepEventArgs^ e);
    	public: event CpuStepEventHandler^ CpuStepEvent;
    	protected: virtual void OnCpuStepEvent(CpuStepEventArgs^ e) {
    				   //if(CpuStepEvent != nullptr)
    				   CpuStepEvent(this, e);
    			   }
    I'm firing it like this:
    Code:
    CpuStepEventArgs^ step = gcnew CpuStepEventArgs();
    OnCpuStepEvent(step);
    //next line


    The problem is that the next line seems to be executed before the handler finishes it's work!

    And bizarrely, I sometimes require the EventArgs in a function that fired the event (ie. in that next line) and it seems to hold the changes made by the handler just fine!

    I used a StopWatch to measure the ticks before and after the firing of the event and at the start and end of the handler function. And the figures vary greatly! And they shouldn’t if the event is synchronous right?

    As you can probably guess from the code, the app I’m making is a little cpu emulator. The class that models the cpu fires the event every time it finishes a step (instruction). The handler in a form uses the EventArgs to add a row with the step information to a DataGridView control. If the cpu takes too many steps to finish the rows don't show until it's done, and all you can see while it's working is that the vertical scroll of the grid is getting thinner - indicating that the rows are being added. I just can't see 'em until it's all done! I've tried using Invalidate() on the form, grid and the new row itself w/o success.

    I'm most probably doing something wrong, but I'm pretty new at this 'event driven' malarkey so I'm stumped. Please help? Thanks in advance!

    PS. I don't know if it matters, but I'm actually creating the EventArgs in one function and pass them to another one that does the actual firing of the event.

    PPS. 'Synchronous' means that everything is nice and sequential right? I might have gotten the term wrong, but I hope it's clear what I'm after.
  • BahatiSiD
    New Member
    • Oct 2008
    • 9

    #2
    Got it!

    When you think about it (which I was apparently not able to do at five AM) there had to be something going on in the handler which is not immediately done - i.e. drawing the new row in the DataGridView.

    The 'secret' was the Refresh() method of the dgv which forces it to redraw immediately.

    My little cpu is now, of course, painfully slow, but that's kind of the whole point - to be able to SEE it work.

    All this drawing makes my GUI pretty unresponsive so I will probably have to go the dreaded threading route. Just a heads up, 'cos I'm bound to have same more lame questions in the wee hours of the morning.

    Thanks to anyone that read this!

    Comment

    Working...