Problems with Threads and Invokes
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Problems with Threads and Invokes

  1. #1
    Join Date
    Jun 2010
    Posts
    4

    Problems with Threads and Invokes

    Dear C#-experts!

    I have a big problem and I hope you will give me support.

    My problem:

    Class A is a windows form containing a listbox. A method of this class A starts a thread, which is responsible for loading of a very huch amount of data lines. These lines have to be added in realtime to the listbox of Class A. The functionality of this thread is implemented in a class B.

    I implemented a delegate method to fill the listbox (owned by class A) via class B.
    I did it like the MSDN proposes: --> http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx

    The difference to the MSDN example is: In class B the lines to be added to the listbox of class A are gathered and an array of ap. 1000 lines is sent to the listbox of class A via the delegate method. After that the thread pauses for 500 milliseconds. In the meanwhile the delegate method processes this data array and adds the array items to the listbox.

    And now trouble comes into paradise:

    In some cases my coding runs into an exception: The invoke of the delegate method to fill the listbox of class A doesn’t work. The exception message is (translated): “The value may not be NULL. Name of the parameter: item.”
    (I get the german message: „Der Wert darf nicht NULL sein. Parametername: item”.)

    Stack trace:
    System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
    System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
    and so on…..

    Could it be a runtime topic?????????? This is my assumption, but without ideas whether I’m right or not and how to solve the problem:

    Assumption:
    The main thread (class A) is adding 1000 items to the listbox and the thread running in class B calls the delegate method of class A to add the next 1000 items in this period of time. The delegate method is called to fill the listbox again while the delegate method from the call before is just working.

    Do you have an idea how to get rid of this problem?

    Thanks a lot in advance!!!

    Paula

  2. #2

    Re: Problems with Threads and Invokes

    It would appear that you are making your problem overly difficult. The error message means exactly what it says

    somewhere in your code you must have mylistbox.Items.Add(someitem) and someitem is null, that is what is causing the exception.

    use a tracepoint that will break execution when "someitem" is null.

    http://msdn.microsoft.com/en-us/library/232dxah7.aspx

  3. #3
    Join Date
    Jun 2010
    Posts
    4

    Re: Problems with Threads and Invokes

    Dear Mathew,

    You are great! With your advise I could fix the bug! During parsing of very large files I added the file contents into an array and called the delegate function to add the array items into a listbox control.

    This array contained some “null” items I couldn’t identify up to now, because the array is very large and I was really blind to see the horrible array item with the content “null”!!!!!

    I was too stupid to understand the meaning of the exception message and looked for runtime problems, but with your expertise I could learn a lot and could fix the bug! Thanks! Now I have to debug my SW to find the situation where an array item with “null” content is added...

    At the moment a quick and dirty workaround inside the delegate method avoids the exception and all is working fine :-)

    Many, many thanks!!!!

    Paula

  4. #4

    Re: Problems with Threads and Invokes

    I am glad that I could be of help. You have no idea how much help I have gotten from "gurus" in the past. After going on 15 years of reading error messages I can nail one everyonce in a while, you will get there too with time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center