CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Jan 2007
    Posts
    32

    c# Threading hell (should be simple)

    Hi,
    I *really* need some help, please. I've been researching for hours about how to invoke methods 'safely', but I just can't do it. EVERY, and I mean every, method I've found throws an error.

    First, making the thread is easy:

    Code:
    private void btnStart()
    {
    
       Thread scanThread = new Thread(new ThreadStart(startScan));
         scanThread.Start();
    }
    No problem there. Next, here is the startScan() method that gets its own thread (on scanThread)

    Code:
    private void startScan()
    {
     //lots of code
     //lots of code
    
     addToPortfolio(q);
    }
    Now, addToPortfolio() gives me the trouble. Essentially it just takes 'q' (which is a custom type) and adds it to a listview

    Code:
    private void addToPortfolio(Quote q)
    {
      listview1.items.add(q.Price);
      listview1.items.add(q.Volume);
    }
    Of course, I get the cross thread exception. No problem, I'll try to invoke it:

    Code:
       private void addPortfolio(Quote q)
            {
    
                if (InvokeRequired)
                {
                    this.Invoke(delegateAddPortfolio(q));
                    
                }
                   
                
            }
    And of course, the delegate sub is:

    Code:
      private delegate void delegateAddPortfolio(Quote q)
            {
               //listview code here
    }

    But it doesn't like the 'delegate' word, it throws over 100 errors!

    If I remove the delegate word, then I get this error:

    Argument '1': cannot convert from 'void' to 'System.Delegate'

    Please, someone just get this working for me. Can someone please assist me?

    P.S. , I've tried this too!

    Code:
     this.Invoke(delegateAddPortfolio, new object [] {q});
    Then I get "Argument '1': cannot convert from 'method group' to 'System.Delegate'"

    Thank you so much.
    Last edited by rsagona1; January 30th, 2010 at 01:28 AM.

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: c# Threading hell (should be simple)

    try
    Code:
    private delegate void delegateAddPortfolio(Quote q);
    private void addToPortfolio(Quote q)
    {
      if (this.InvokeRequired){
        this.Invoke(new delegateAddPortfolio(addToPortfolio), q);
      }
      else{
        listview1.items.add(q.Price);
        listview1.items.add(q.Volume);
      }
    }

  3. #3
    Join Date
    Jan 2007
    Posts
    32

    Re: c# Threading hell (should be simple)

    You...are..the..man!! THANK YOU!!! You just saved me a ton of time and money.
    Last edited by rsagona1; January 30th, 2010 at 07:36 PM.

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

Featured