473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Events, Threads and ISynchronizeInvoke

GM
Hi,

My application has a need to cache a number of shared reference lists
containing basic business objects. In order to improve performance these
lists are fetched and updated in the background.

When a UI is required to display one of these reference lists it requests it
by name from the cache. If the list already exists in the cache it is
returned to the caller otherwise a brand new empty one is created and
returned. This new empty list then initiates a background thread to fetch
the items from the database. On return the contents of the list are merged
with the empty list in the cache and the appropriate ListChanged events
raised to notify observing controls that the list has changed.

I use Joval Lowy's BackgroundWorker
(http://www.idesign.net/idesign/deskt...ndex=5&tabid=8) to
perform the background fetch.

My problem is that the merge/update to the cached list takes place on the
background thread. This results in a ListChanged event being raised on that
thread and the control that the list is bound to being updated on a thread
other than the one that created it. I would like to be able to return to the
main thread when the background thread has finished its work of fetching the
list. This would allow the main thread to perform the update of the cached
list and the raising of the ListChanged event. I have read about the
ISynchronizeInvoke interface and thought that that would be the way to go
but I cannot see how to marshall back to the main thread other than to use
PostMessage.

Am I missing something obvious?

Thanks in advance,

Graham
Jul 21 '05 #1
9 2192
GM <ma*****@NOSPAMgaelquality.co.uk> wrote:
My application has a need to cache a number of shared reference lists
containing basic business objects. In order to improve performance these
lists are fetched and updated in the background.

When a UI is required to display one of these reference lists it requests it
by name from the cache. If the list already exists in the cache it is
returned to the caller otherwise a brand new empty one is created and
returned. This new empty list then initiates a background thread to fetch
the items from the database. On return the contents of the list are merged
with the empty list in the cache and the appropriate ListChanged events
raised to notify observing controls that the list has changed.

I use Joval Lowy's BackgroundWorker
(http://www.idesign.net/idesign/deskt...ndex=5&tabid=8) to
perform the background fetch.

My problem is that the merge/update to the cached list takes place on the
background thread. This results in a ListChanged event being raised on that
thread and the control that the list is bound to being updated on a thread
other than the one that created it. I would like to be able to return to the
main thread when the background thread has finished its work of fetching the
list. This would allow the main thread to perform the update of the cached
list and the raising of the ListChanged event. I have read about the
ISynchronizeInvoke interface and thought that that would be the way to go
but I cannot see how to marshall back to the main thread other than to use
PostMessage.

Am I missing something obvious?


I don't know how obvious it is, but Control.Invoke is what you're
after.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
GM
Thanks for replying Jon,

I had written a reply that basically said that I couldn't call
Control.Invoke because the class where I want to return to the main thread
is not a control, its a container for my shared reference lists. But then I
got to thinking why couldn't it be a control? So I made my
ReferenceListCache class inherit from control and when the BackgroundWorker
completed, also in the ReferenceListCache, I attempted to invoke the
MergeList method, also in ReferenceListCache, through the MergeDelegate.
But the MergeList method is never called. So what am I doing wrong? I've
pasted some code below.

public delegate void MergeDelegate(ArrayList list);

public class ReferenceListCache

{

public void MergeList(ArrayList list)

{

mListRegistry.UpdateCachedList(list, true);

}

private void worker_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)

{

ArrayList list = e.Result as ArrayList;
MergeDelegate merge = new MergeDelegate(MergeList);

object[] args = new object[]{list};

this.Invoke(merge, args);

}

}

Thanks,

Graham
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
GM <ma*****@NOSPAMgaelquality.co.uk> wrote:
My application has a need to cache a number of shared reference lists
containing basic business objects. In order to improve performance these
lists are fetched and updated in the background.

When a UI is required to display one of these reference lists it requests
it
by name from the cache. If the list already exists in the cache it is
returned to the caller otherwise a brand new empty one is created and
returned. This new empty list then initiates a background thread to fetch
the items from the database. On return the contents of the list are
merged
with the empty list in the cache and the appropriate ListChanged events
raised to notify observing controls that the list has changed.

I use Joval Lowy's BackgroundWorker
(http://www.idesign.net/idesign/deskt...ndex=5&tabid=8)
to
perform the background fetch.

My problem is that the merge/update to the cached list takes place on the
background thread. This results in a ListChanged event being raised on
that
thread and the control that the list is bound to being updated on a
thread
other than the one that created it. I would like to be able to return to
the
main thread when the background thread has finished its work of fetching
the
list. This would allow the main thread to perform the update of the
cached
list and the raising of the ListChanged event. I have read about the
ISynchronizeInvoke interface and thought that that would be the way to go
but I cannot see how to marshall back to the main thread other than to
use
PostMessage.

Am I missing something obvious?


I don't know how obvious it is, but Control.Invoke is what you're
after.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3
GM <ma*****@NOSPAMgaelquality.co.uk> wrote:
I had written a reply that basically said that I couldn't call
Control.Invoke because the class where I want to return to the main thread
is not a control, its a container for my shared reference lists.


It doesn't need to *be* a control - it merely has to have a *reference*
to a control. If you're wanting to do something with the UI, you must
have a reference to a control of some description somewhere.

Note that you don't have to know that it's a control - Control
implements ISynchronizeInvoke, so you can pass a reference to a control
to something more general which accepts an ISynchronizeInvoke to use to
synchronize when it's finished.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
GM
Thanks Jon.

I had also tried that approach, by giving my ReferenceListCache class a
Label control as a private member. I then try to Invoke the delegate on
this Label control. But the delegate method is still not being called. In
order to test the delegate method I did a DynamicInvoke on the delegate
instead of a Control.Invoke and the method was called. So I think the
problem must be a misunderstanding on my part on the use of Control.Invoke.

Any thoughts?

Thanks,

Graham

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
GM <ma*****@NOSPAMgaelquality.co.uk> wrote:
I had written a reply that basically said that I couldn't call
Control.Invoke because the class where I want to return to the main
thread
is not a control, its a container for my shared reference lists.


It doesn't need to *be* a control - it merely has to have a *reference*
to a control. If you're wanting to do something with the UI, you must
have a reference to a control of some description somewhere.

Note that you don't have to know that it's a control - Control
implements ISynchronizeInvoke, so you can pass a reference to a control
to something more general which accepts an ISynchronizeInvoke to use to
synchronize when it's finished.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #5
GM <ma*****@NOSPAMgaelquality.co.uk> wrote:
I had also tried that approach, by giving my ReferenceListCache class a
Label control as a private member. I then try to Invoke the delegate on
this Label control. But the delegate method is still not being called. In
order to test the delegate method I did a DynamicInvoke on the delegate
instead of a Control.Invoke and the method was called. So I think the
problem must be a misunderstanding on my part on the use of Control.Invoke.


I don't think you can just create a control without displaying it or
adding it to something else which has a window associated with it. Use
one of your *real* controls from the UI as the synchronization object.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6
GM
First of all thanks for all your help Jon.

I'm now passing a Control to my ReferenceListCache and using this Control to
Invoke the appropriate method. This is working great 99% of the time.
However, it is the 1% of the time thats driving me insane. On those
occassions the delegate method is not being called. Instead, as far as I
can make out, a callback method in the BackgroundWorker component is being
called instead (ReportCompletion). I cannot work it out and its behaviour
is not consistent.

If you can shed any light on why this is happening that would be great. If
not, thanks again for your help.

Cheers,

Graham
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
GM <ma*****@NOSPAMgaelquality.co.uk> wrote:
I had also tried that approach, by giving my ReferenceListCache class a
Label control as a private member. I then try to Invoke the delegate on
this Label control. But the delegate method is still not being called.
In
order to test the delegate method I did a DynamicInvoke on the delegate
instead of a Control.Invoke and the method was called. So I think the
problem must be a misunderstanding on my part on the use of
Control.Invoke.


I don't think you can just create a control without displaying it or
adding it to something else which has a window associated with it. Use
one of your *real* controls from the UI as the synchronization object.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #7
GM <ma*****@NOSPAMgaelquality.co.uk> wrote:
First of all thanks for all your help Jon.

I'm now passing a Control to my ReferenceListCache and using this Control to
Invoke the appropriate method. This is working great 99% of the time.
However, it is the 1% of the time thats driving me insane. On those
occassions the delegate method is not being called. Instead, as far as I
can make out, a callback method in the BackgroundWorker component is being
called instead (ReportCompletion). I cannot work it out and its behaviour
is not consistent.

If you can shed any light on why this is happening that would be great. If
not, thanks again for your help.


That sounds very odd... Could you post a short but complete program
which demonstrates the problem? (Even occasionally?)

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #8
GM
Jon,

I tried to create a simple program and recreate the problem but I could not.
But I have managed to get it working although I'm not entirely sure how.
The prolem appears to be the fact that the creation of the custom control
(which fetches the 3 lists in its constructor) is done in the
InitializeComponent method of the form. When the creation of this custom
control is delayed (until another container control is expanded) the
delegate method is Invoked correctly each time and the lists are returned.
So I would hazard a guess that because the Window has not been created
completely, it is not receiving Windows messages correctly. But as I say,
thats only a guess.

Thanks for all your help,

Graham

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
GM <ma*****@NOSPAMgaelquality.co.uk> wrote:
First of all thanks for all your help Jon.

I'm now passing a Control to my ReferenceListCache and using this Control
to
Invoke the appropriate method. This is working great 99% of the time.
However, it is the 1% of the time thats driving me insane. On those
occassions the delegate method is not being called. Instead, as far as I
can make out, a callback method in the BackgroundWorker component is
being
called instead (ReportCompletion). I cannot work it out and its
behaviour
is not consistent.

If you can shed any light on why this is happening that would be great.
If
not, thanks again for your help.


That sounds very odd... Could you post a short but complete program
which demonstrates the problem? (Even occasionally?)

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #9
GM <ma*****@NOSPAMgaelquality.co.uk> wrote:
I tried to create a simple program and recreate the problem but I could not.
But I have managed to get it working although I'm not entirely sure how.
The prolem appears to be the fact that the creation of the custom control
(which fetches the 3 lists in its constructor) is done in the
InitializeComponent method of the form. When the creation of this custom
control is delayed (until another container control is expanded) the
delegate method is Invoked correctly each time and the lists are returned.
So I would hazard a guess that because the Window has not been created
completely, it is not receiving Windows messages correctly. But as I say,
thats only a guess.


That would sound fairly reasonable to me, yes...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #10

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

Similar topics

2
by: JSheble | last post by:
Say I have two seperate timers in my app (it's strictly an example, I don't really...) and each of the timer events calls the same method of the form's class. Will these interfere with each other...
10
by: Drakier Dominaeus | last post by:
This is my first time posting here, so please forgive me if I do anything incorrectly. I've been learning C# and working with different things and decided I wanted to get into Multi-Threading....
3
by: Jacob | last post by:
I'm working on a class that needs to be called from a windows form, do it's work, and then, show progress back to the main form. I'm well aware that worker threads need to call Invoke for updates...
1
by: Bruce M. Carroll | last post by:
I am doing some work a distributed application, which uses events and remoting to accomplish the signaling between applications. This all works. The problem I have (I think) is that since...
5
by: Richard | last post by:
All, I have a worker thread that fires events across threads to both GUI objects and thread agnostic objects. My code is working but I want to be assured that it did it "the right way"... ...
2
by: Stampede | last post by:
Hi guys 'n' girls, I want to use callback methods when using BeginInvoke on some events. So far no problem, but know I thought about what could happen (if I'm not completly wrong). Lets say an...
4
by: Manuel | last post by:
I have a long function that needs to be done 1000 times. I'm multithreading it, but I don't want to load them up all at once, instead load them 10 at a time. So far the only way I can get it to...
9
by: GM | last post by:
Hi, My application has a need to cache a number of shared reference lists containing basic business objects. In order to improve performance these lists are fetched and updated in the...
15
by: Bryce K. Nielsen | last post by:
I have an object that starts a thread to do a "process". One of the steps inside this thread launches 12 other threads via a Delegate.BeginInvoke to process. After these 12 threads are launched,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.