Re: Populate an ImageList from another thread

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Sat, 15 Dec 2007 11:12:15 -0800, Greg Cadmes <gcadmes@xxxxxxxxx> wrote:

Becase your listview was created by your form's gui thread, by saying
Control.Invoke
is the same as ListView.Items.Add.... syncrhonously. The calling
thread still needs to wait for the Invoke() function to complete until
the control is returned to the calling thread.

Yes, it is a synchronous call. But as long as the main GUI thread isn't waiting on the thread that is trying to make the synchronous call, it's not a problem.

Conversely, if it _is_ a problem to make the synchronous call, then there is an underlying problem with the design of the code that is likely fixed in a better way. There are good reasons to use BeginInvoke() instead of Invoke(), but working around a deadlocking design bug isn't one of them.

If you look at the sample code I provided to the OP (contrary to your apparent misunderstanding, I'm not the OP, nor do I have code that isn't working correctly), it is quite possible and reasonable to use Invoke() rather than BeginInvoke().

This is not a .NET bug.

You don't know that. There's no way for you to know that, because you haven't seen the code that doesn't work, and neither have I.

Furthermore, the fact that code that is known to be correct (see the sample I posted) apparently does not work on the OP's installation suggests that at the very least, there's something wrong with his installation. And it may well be that whatever's wrong with his installation is simply due to the specific version he has, and that there is indeed a bug in that version of .NET.

I use the APM (Asynchronous Programming Model)
in almost every project I work on.
Take a look at this example of asynchronous method invocation:
https://secure.codeproject.com/KB/cs/AsyncMethodInvocation.aspx

I'm sure you'll see that this is the right approach for the creating
the tiled bitmaps.

While I'm very familiar with asynchronous programming, both in the context of .NET and without, I disagree that this particular situation is one in which using an asynchronous invoke is required or even desirable.

In situations where there _is_ some kind of potential deadlock problem, switching to BeginInvoke() typically results in all of the operations being queued up until some later time. Because it's a situation that otherwise would deadlock, that "later time" usually winds up being "not until the background thread has completed its work", which means that those updates all come at once, after the background thread is done.

In other words, using BeginInvoke() winds up accomplishing pretty much the same thing that would happen as if the work were all done synchronously in the main thread.

Now, I don't know about you, but IMHO it doesn't really make much sense to use a background worker thread when the net effect winds up being identical to that had one just done everything synchronously in the main GUI thread. That's a pointless use of background worker threads.

Pete
.



Relevant Pages

  • Re: drawstring disappears?
    ... I invoke this function from a background worker thread every second. ... My doWork function in my background worker thread is basically the following: ... I'm guessing it has something to do with that Thread.Sleep, but does anybody know how I can update my progress bar text without having the text seem to flicker? ...
    (microsoft.public.dotnet.general)
  • Re: drawstring disappears?
    ... I invoke this function from a background worker thread every second. ... This seems to definately be affected by the background worker thread sleeping. ... If I change the sleep time to be 10 seconds ), then the text will disappear for 10 seconds, reappear for a split second, and repeat that process. ...
    (microsoft.public.dotnet.general)
  • Re: worker thread shutdown and Form.Invoke
    ... Stephen Lamb wrote: ... > I have a background worker thread which I start from a form's HandleCreated ... > event that makes calls back to the form using Invoke. ... The flag is set in Form.Closing. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: When is Invoke() Really Required?
    ... then it is safe to call that method. ... in the context of the calling thread, now if the calling thread does not ... > Invoke, BeginInvoke, EndInvoke and CreateGraphics. ... Does this apply for absolutely every method or property in a control ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: INVOKE (English vs COBOL
    ... William M. Klein wrote: ... >still hoping for a '97 Standard), I wrote a number of papers on WHY I thought ... Calling a method is like calling a ... The term Invoke a Method is also used. ...
    (comp.lang.cobol)