Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. BeginInvoke

BeginInvoke

Scheduled Pinned Locked Moved C#
winformsdata-structuresperformancequestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hugo Hallman
    wrote on last edited by
    #1

    I can't find the equivalent to PostMessage in Windows Forms. I'm currently using BeginInvoke, as it's much like it, but then I get memory leaks, since I ignore the AsyncResults. What should I do with them? Store them in a queue which I walk through later to see if the Completedflag is set and can then safelly call endinvoke? Thanks in advance

    H 1 Reply Last reply
    0
    • H Hugo Hallman

      I can't find the equivalent to PostMessage in Windows Forms. I'm currently using BeginInvoke, as it's much like it, but then I get memory leaks, since I ignore the AsyncResults. What should I do with them? Store them in a queue which I walk through later to see if the Completedflag is set and can then safelly call endinvoke? Thanks in advance

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You won't get memory leaks if you ignore AsyncResults. .NET is GC'd. More than likely you're not using Control.InvokeRequired and Control.Invoke to call methods and get/set properties on controls created in a different thread. This is a common problem. When you comunicate with a control that was created on a different thread (the UI thread), you must use the ISynchronizeInvoke impementation that every control inherits from Control. This makes sure that communciate with the control happens in the UI thread. If you want PostMessage, why not just P/Invoke it? If you read the forms, we're continually telling people to P/Invoke SendMessage to send the message immediately, but P/Invoking PostMessage is really no different. It would just look like this:

      [DllImport("user32.dll")]
      private static extern bool PostMessage(
      IntPtr hWnd,
      [MarshalAs(UnmanagedType.U4)] int msg,
      IntPtr wParam,
      IntPtr lParam);

      If you need to pass a struct or reference to a struct or something, instead of marshaling the struct yourself and passing it as an IntPtr, you could overload PostMessage like so:

      [DllImport("user32.dll")]
      private static extern bool PostMessage(
      IntPtr hWnd,
      [MarshalAs(UnmanagedType.U4)] int msg,
      int wParam, // DON'T NEED - SET TO 0
      ref LVITEM lvItem);

      Of course, you'd need to declare a managed LVITEM struct (can be named whatever you like, though) that would marshal correctly.

      Microsoft MVP, Visual C# My Articles

      H 1 Reply Last reply
      0
      • H Heath Stewart

        You won't get memory leaks if you ignore AsyncResults. .NET is GC'd. More than likely you're not using Control.InvokeRequired and Control.Invoke to call methods and get/set properties on controls created in a different thread. This is a common problem. When you comunicate with a control that was created on a different thread (the UI thread), you must use the ISynchronizeInvoke impementation that every control inherits from Control. This makes sure that communciate with the control happens in the UI thread. If you want PostMessage, why not just P/Invoke it? If you read the forms, we're continually telling people to P/Invoke SendMessage to send the message immediately, but P/Invoking PostMessage is really no different. It would just look like this:

        [DllImport("user32.dll")]
        private static extern bool PostMessage(
        IntPtr hWnd,
        [MarshalAs(UnmanagedType.U4)] int msg,
        IntPtr wParam,
        IntPtr lParam);

        If you need to pass a struct or reference to a struct or something, instead of marshaling the struct yourself and passing it as an IntPtr, you could overload PostMessage like so:

        [DllImport("user32.dll")]
        private static extern bool PostMessage(
        IntPtr hWnd,
        [MarshalAs(UnmanagedType.U4)] int msg,
        int wParam, // DON'T NEED - SET TO 0
        ref LVITEM lvItem);

        Of course, you'd need to declare a managed LVITEM struct (can be named whatever you like, though) that would marshal correctly.

        Microsoft MVP, Visual C# My Articles

        H Offline
        H Offline
        Hugo Hallman
        wrote on last edited by
        #3

        Thank you very much for your answer. Although I don't agree on your discussion, I'll use the code you gave me. I tested calling BeginInvoke(Nada) a hundred thousand times and then GC.Collect(); The memory footprint after the collection was over a hundred megs. ??? And the reason I ask is that I do use BeginInvoke, but am not satisfied with it. And Invoke is no option since it causes deadlocks in my case. (The UI is updated from a background process.)

        H 1 Reply Last reply
        0
        • H Hugo Hallman

          Thank you very much for your answer. Although I don't agree on your discussion, I'll use the code you gave me. I tested calling BeginInvoke(Nada) a hundred thousand times and then GC.Collect(); The memory footprint after the collection was over a hundred megs. ??? And the reason I ask is that I do use BeginInvoke, but am not satisfied with it. And Invoke is no option since it causes deadlocks in my case. (The UI is updated from a background process.)

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Doesn't really matter if you don't agree with me - it's documented in the .NET Framework SDK. I suppose, then, that you disagree with Microsoft. I didn't say that ignoring the AsyncResults doesn't incur a certain performance hit with memory consumption, I said it didn't incur any memory leaks. There is a difference. The very fact that the GC was able to collect the unused references proves that the objects where indeed tracked. The GC would've collected them eventually. Memory leaks are when memory is lost and unreferenced, so it cannot be freed again, like when a variable references an alloc'd region of memory which is not freed. Once that variable goes out of scope it's practically impossible to free that memory again. The fact remains that calling methods or setting properties (often getting as well) on controls from a different thread may cause problems. This is what's documented well in the .NET Framework if you read the right topics. Sometimes things may work. Other times they may not. Often times it is certain functionality that doesn't work as expected. It call comes down to the synchronization of threads, like it or not.

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups