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 BackgroundWorke r
( 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
ISynchronizeInv oke 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 9 2225
GM <ma*****@NOSPAM gaelquality.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 BackgroundWorke r (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 ISynchronizeInv oke 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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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
ReferenceListCa che class inherit from control and when the BackgroundWorke r
completed, also in the ReferenceListCa che, I attempted to invoke the
MergeList method, also in ReferenceListCa che, 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(A rrayList list);
public class ReferenceListCa che
{
public void MergeList(Array List list)
{
mListRegistry.U pdateCachedList (list, true);
}
private void worker_RunWorke rCompleted(obje ct sender,
RunWorkerComple tedEventArgs e)
{
ArrayList list = e.Result as ArrayList;
MergeDelegate merge = new MergeDelegate(M ergeList);
object[] args = new object[]{list};
this.Invoke(mer ge, args);
}
}
Thanks,
Graham
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... GM <ma*****@NOSPAM gaelquality.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 BackgroundWorke r (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 ISynchronizeInv oke 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.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
GM <ma*****@NOSPAM gaelquality.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 ISynchronizeInv oke, so you can pass a reference to a control
to something more general which accepts an ISynchronizeInv oke to use to
synchronize when it's finished.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Thanks Jon.
I had also tried that approach, by giving my ReferenceListCa che 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 misunderstandin g on my part on the use of Control.Invoke.
Any thoughts?
Thanks,
Graham
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... GM <ma*****@NOSPAM gaelquality.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 ISynchronizeInv oke, so you can pass a reference to a control to something more general which accepts an ISynchronizeInv oke to use to synchronize when it's finished.
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
GM <ma*****@NOSPAM gaelquality.co. uk> wrote: I had also tried that approach, by giving my ReferenceListCa che 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 misunderstandin g 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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
First of all thanks for all your help Jon.
I'm now passing a Control to my ReferenceListCa che 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 BackgroundWorke r component is being
called instead (ReportCompleti on). 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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... GM <ma*****@NOSPAM gaelquality.co. uk> wrote: I had also tried that approach, by giving my ReferenceListCa che 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 misunderstandin g 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.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
GM <ma*****@NOSPAM gaelquality.co. uk> wrote: First of all thanks for all your help Jon.
I'm now passing a Control to my ReferenceListCa che 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 BackgroundWorke r component is being called instead (ReportCompleti on). 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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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
InitializeCompo nent 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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... GM <ma*****@NOSPAM gaelquality.co. uk> wrote: First of all thanks for all your help Jon.
I'm now passing a Control to my ReferenceListCa che 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 BackgroundWorke r component is being called instead (ReportCompleti on). 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.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
GM <ma*****@NOSPAM gaelquality.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 InitializeCompo nent 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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 at all? Will they run
as if they're threaded?
Example:
private void timer1_Tick(object sender, System.EventArgs e)
{
this.SomeMethod()
|
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. My problem is that I must not be doing it right
because only some of the stuff works as would be expected. I'll post what
exactly is happening, then...
|
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 to the main thread to be
threadsafe. I want to make this worker class I'm writing a self contained
assembly so that other's can drop it into their...
|
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 remoting/events essentially
guarantees that you are working in a multi-threaded environment, the
developer of the application that consumes these events,...
|
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"... Question: Is there a better way?
According to the .NET docs that I read all that I have to do to fire my
custom "OnSynchronizationStatusChange()"...
| |
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 event is triggerd with BeginInvoke and a callback funktion which
is part of the instanze which is also holding the event. So the call looks...
|
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 work is by creating a dummy form
with a timer. On the timer function I test if the number of threads are
less than 10 then run the remaining ones....
|
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 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...
|
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, the main thread waits. At the
completion of each subthread, the mainthread checks all 12 thread objects to
see if they are done. If they are, raise an...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| | |