Results 1 to 6 of 6

Thread: Background Thread causing System.Reflection.TargetInvocationException

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Background Thread causing System.Reflection.TargetInvocationException

    I set up a background thread to show progress information on the screen while my main process is running. I am updating one of three datagridviews, and adjust a progress bar in each call to the background thread. I believe that I am getting a collision in different calls to the worker.ReportProgress. That is I think a new call is arriving before a prior call finishes, throwing the System.Reflection.TargetInvocationException.

    Any suggestions on how to resolve this? If this is the problem, then minimizing the time in in the ReportProcess procedure, or lengthening the time between calls would fix the problem, but this seems arbitrary.

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,388

    Re: Background Thread causing System.Reflection.TargetInvocationException

    not much can be said without you posting some stripped down code to illustrate and ideally reproduce the problem. if it is a cross thread issue then lengthening or shortening some time is defenitely not the right way to fix anything.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Background Thread causing System.Reflection.TargetInvocationException

    I am trying to shift some of the processing out of the background procedure and see if that takes care of my problem. If that does not work I will likely come back and post some stripped down code.

  4. #4
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,388

    Re: Background Thread causing System.Reflection.TargetInvocationException

    you need to be aware of what ressources (objects etc) you access from what thread and that any thread can be interupted anytime. so you must take care of conflicts in accessing shared ressources.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Background Thread causing System.Reflection.TargetInvocationException

    I am dealing only with progress bars and datagridviews.
    I only access the progress bar in the background thread so that should be clean
    I make make all changes to the dgv in the background thread. i pass in the fields and add rows in the background. I also am changing some colors of cells in the background. I the foreground I do get the number of rows in the background and do read information out of the dgv.

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: Background Thread causing System.Reflection.TargetInvocationException

    Quote Originally Posted by John_SC View Post
    I am dealing only with progress bars and datagridviews.
    I only access the progress bar in the background thread so that should be clean
    I make make all changes to the dgv in the background thread. i pass in the fields and add rows in the background. I also am changing some colors of cells in the background. I the foreground I do get the number of rows in the background and do read information out of the dgv.
    That is pretty much your problem then, you are accessing user interface elements from a background thread. This is a big no-no, you can only access the UI from the main UI thread.

    http://www.vbforums.com/showthread.p...ding-in-VB-Net is probably something you will find worth a read.
    Last edited by PlausiblyDamp; Mar 18th, 2017 at 02:32 PM.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width