C# events and delegates from thread RRS feed

  • Discuţie generală

  • Hello,

    I have some problems with some events and delegates in C#. I hope you can help me . I'm trying to see why i get two error messages in an application. This is a context.

    In the application exist the following entities:

    1. A Pool with data. Here all the data from the database is saved for fast access in a Dictionary data structure. Every element from the pool is identified based on the unique id number.

    2. A TreeView And a GridView, where the data from the Pool is displayed.

    3. An Update Thread that every 60 seconds scans the database for changes and sends events to the Pool with these changes. The Pool is going to call the UI elements that are registered to it in order to make them update the data displayed. The Thread can send in the event args two types : argTypeA, argTypeB.

    So, to summarize, when the application starts :

    1. The Pool is created.

    2. The Pool creates the UpdateThread, and registers to it's event.

    3. The UI components are created: tree and grid . Both of these two elements are going to register to the pool in order to be signalized when the event from the update thread arrives. They register in the classic observer way : the delegates are added to an arrayList in the Pool.

    When update thread sends event with argTypeA, the Pool knows to iterate in the arrayList where the UI delegates are and for every one of them to make a call : myDelegate.DynamicInvoke(new object[] { this, args }); The scope is to update a single element from the Pool.

    When update thread sends event with argTypeB, the Pool knows to update itself: every element is going to take a default value.

    The thread is going to send the events (let's make it a little simple) in the following order : with argsTypeA and the with argsTypeB.

    So, the Pool receives the event with argsTypeA and start iterating the subscribers(tree and grid) and update the single element. When the tree updates itself, it makes an access to the pool and updates there also the single element. Now it's the grids turn, but the grid detects that the element is loaded and does nothing. After this, the pool sends the event with argsTypeB, that is going to reset the pool.

    My problem is the following : threads sends argsTypeA, tree updates (also the element from the pool), the grid also updates (also the element from the pool) --> Why ? The data has been updated by the tree. This update is not required anymore !!!.

    From my analysis, i think that this is what happens: thread sends argsTypeA, tree update(also the element from the pool), iteration in the arrayList pauses, thread sends argsTypeB, pool is reset, iteration is continued, grid is updated also(because the pool was reset). Can something like this happen, because it is quite stange. Also i've read a lot about async calls and delegates but no articles reveals what can happend. Please help.....


    P.S : i don't know if i can post here in romanian also, but you are free to write in romanian :)

    marți, 9 noiembrie 2010 15:06