473,513 Members | 2,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Executing code on parent thread

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
Apr 26 '06 #1
4 3937
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
"sklett" <sk****@mddirect.com> wrote in message
news:OI**************@TK2MSFTNGP02.phx.gbl...
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

Apr 26 '06 #2
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!
"Greg Young [MVP]" <Dr*************@hotmail.com> wrote in message
news:ul**************@TK2MSFTNGP02.phx.gbl...
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
"sklett" <sk****@mddirect.com> wrote in message
news:OI**************@TK2MSFTNGP02.phx.gbl...
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


Apr 26 '06 #3
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...delegates.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.

"sklett" <sk****@mddirect.com> wrote in message
news:%2***************@TK2MSFTNGP03.phx.gbl...
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!
"Greg Young [MVP]" <Dr*************@hotmail.com> wrote in message
news:ul**************@TK2MSFTNGP02.phx.gbl...
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
"sklett" <sk****@mddirect.com> wrote in message
news:OI**************@TK2MSFTNGP02.phx.gbl...
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



Apr 26 '06 #4
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.
"sklett" <sk****@mddirect.com> wrote in
news:#e*************@TK2MSFTNGP03.phx.gbl:
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!

Apr 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
42509
by: Asad | last post by:
Hi, I am basically trying to accomplish drop down menus for navigation on a site. And I'm pretty much done except I am having one problem. The z-index is not working with relative positioning! ...
13
2811
by: BK | last post by:
Can someone point me to a code sample that illustrates executing long running tasks (asynchronous) from a web application in ASP.NET? I assume that Web Services might come into play at some point,...
5
2484
by: Dayne | last post by:
Can a Parent thread catch a event send by a child thread? Dayne
2
9752
by: Tony Liu | last post by:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect...
0
1481
by: BasicQ | last post by:
I am running an executable from my aspx page with the click of a button. A date is passed as an argument. I am able to get the standardoutput from the Process(Exe) into the label of my page after...
1
2011
by: Jon | last post by:
My question is: Can the Garbage Collector (GC) suspended a managed thread while it is executing native code. The reason I am interested in this is that I have: 1) a native thread (N) that only...
6
1865
by: Eric_Dexter | last post by:
Instead of creating my buttons and waiting for me to press them to execute they are executing when I create them and won't do my callback when I press them.. thanks for any help in advance ...
1
1558
by: Sebouh | last post by:
Hello guys. This time, i need to implement waitpid in order to know if one of the children of a parent has finished executing. The assignment requires me to run a limited number of child processes....
0
1521
Jacotheron
by: Jacotheron | last post by:
Hi I have a database which should return data of the threads. The idea is to create a small community (only selected users may participate) where they can post their statements etc for others to see...
0
7175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7391
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7553
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1609
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.