mHelpMe
mHelpMe

Reputation: 6668

multi threading and raising events

I have an application written in c#. I have a class (say called ClassA) that has a simple event (code below). The event is raised to keep the user updated on the progress of the code.

    public delegate void MyDelegateProgessUpdate(string value);
    public event MyDelegateProgessUpdate ProgressUpdate;

When the application is running a list of ClassA objects are created (up to a maximum of 8). So all 8 objects will be raising an event (in their own class) - this works fine as I have just tested it. However I was wondering if two objects raised an event at the same time if there would be an issue?

Edit

I should have mentioned the following details. The list of ClassA objects will all be running at the same time so it is possible the events could be raised at the same time.

 Task[] myTasks = new Task[numThreads];

 for (int i = 0; i < myTasks.Length; i++)
            myTasks [i] = Task.Factory.StartNew(ClassA[i].DoSomeWork, someData[i]);

This is all being done on a background worker thread. When an event is raised a property on my WPF UI is updated and its OnPropertyChanged event called.

Upvotes: 0

Views: 173

Answers (2)

Patrick Allwood
Patrick Allwood

Reputation: 1832

How will two objects raise events at the same time? From the code and description you have provided, the is nothing to suggest this is actually multi-threaded code, and your events will be processed in the order that they are raised.

If you do have multiple threads, the important thing to remember is that the event handlers will be run on the thread that raised the event, so be careful if you need to modify any user interface items, and synchronize any access to resources being shared between the threads.

Edit:

I can't really comment with regards to WPF, as my experience is mostly Winforms, however this answer appears to pretty comprehensive with regards to managing cross-thread stuff in WPF.

Other than that I wouldn't expect you to have an issue of the events were raised simultaneously, provided they are not sharing a single set of data between them.

Upvotes: 1

Thomas
Thomas

Reputation: 2984

The answer depends solely on the code (your code) that is executed by the events:

IF the code itself is threadsave then no there is no issue. If the code is not 100% threadsave then yes you have a possible problem at your hands (a racing condition to happen).

By itself there is no intricate problem with multiple objects raising the same event. It only depends on the code of the event itself and if the objects have the rights to raise the event. For example if you are in your own thread and try to raise an event on the GUI which runs in its own thread, then you need to use invoke else you will have a problem

That is if you have code that raises events pretty fast (like multiple handlers set to one event, ....) where your code example doesn't go into details there that show how the events are raised (and how you raise the multiple events)

Upvotes: 1

Related Questions