Executing code on parent thread

S

sklett

I'm fairly new to Threading and things have been going pretty smooth so far.
I did some performance testing on my new app and realized it's lagging a bit
on raising events. It's a realtime app and I need to really make sure that
I'm not falling behind (hence the multi threads) so I determined what was
causing the lag and it's absolutely the events I'm raising and possibly also
the UI work they are doing.

Now, from what I can tell from the framework I'm using (Composite
Application Block) once I'm publishing (raising) the events, they are
handled in the UI thread, but still, I have lag.

Here's my basic situation:
<code>
// class SomeProcessingClass...
public event EventHandler ProcessStarted;
public event EventHandler ProcessComplete;

// the UI calls Something() and Something() starts a thread to run the
lenghtly process...
public void Something()
{
// code to start a new thread and start SomethingProc()
}

public void SomethingProc()
foreach(Item i in items)
{
if(ProcessStarted != null)
ProcessStarted(this, EventArgs.Empty);

DoLongProcess(i);

if(ProcessComplete != null)
ProcessComplete(this, EventArgs.Empty);
}
}

DoLongProcess(Item i)
{
Thread.Sleep(2500);
}
</code>

That was pseudo code, but it's basically what I'm doing. If I comment out
the events, I pretty much hit my real-time performance needs (expect 10
seconds and it actually takes 9.994645)

BUT, I don't want to give up my UI feedback as I'm doing this processing. I
thought that maybe I could increase speed by raising the events on the
thread that Something() was called on rather than SomethingProc() which is
the real performer that needs to not be slowed down by anything.

I can't figure out how to do this.... I don't even know if it's possible.
It seems like any function calls from thread 'B' will execute on thread 'B',
I need to make a call from thread 'B' and make it execute on thread 'A'

Any tips?

Thanks for reading!
Steve
 
G

Greg Young [MVP]

Anything that is touching your UI has to be run on your main UI thread (the
thread that created the controls). If you try to do it from any other thread
you will receive exceptions. To help boost your performance ... you can use
BeginInvoke() to push the events to the main thread as opposed to Invoke()
which blocks the background thread until the main thread has completed the
operation.

Cheers,

Greg Young
 
S

sklett

Hi Greg,

Thanks for the post. I realize now that I should have elaborated on my
mention of the "Composite Application Block" framework. I'm using
traditional events in my worker class, but the subscriber to those events is
using a CAB system called the EventBroker to fire them on the UI thread,
it's being handled for me. :0)

I just want to make the worker class not fire events on it's "worker"
thread, but rather the thread it was originally called on.

Hope that makes sense....? I'm even a little bit confused!
 
G

Greg Young [MVP]

The UI events HAVE to be issued on the main thread so I assume that they are
being brokerred off to the main thread.

The more general question here though is how to make a function call from
Thread B run on Thread A (you would need to allow the queuing of messages
back to Thread A) ... Often times people just use the thread pool for this
(they don't really care that they are running on a specific thread, they
only care that they are running on a different thread than the one they were
currently in) this can be done by using a delegate's BeginInvoke method
http://www.ondotnet.com/pub/a/dotnet/2003/02/24/asyncdelegates.html

In specific ... it sounds to me like what is happenning is that your events
are being sent to the main UI thread through an Invoke() which will block
the worker thread. The BeginInvoke() method will allow you to do this
asynchronously.

I have to admit my CAB usage has been minimal ... so I can't tell you with
certainty the method of fixing it in that context ... I just tried to
download the source so I could look at the eventbroker and verify my
suspicions but I was having temporary trouble with a passport sign in;
hopefully I can get it in a bit.
 
P

peancor

Hi,
i think that you should use ThreadOption Publisher on your
eventsubscription, and from the event handler, marshall the call to the
ui thread using BeginInvoke as Greg said.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top