BackgroundWorker with anonymous methods ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: BackgroundWorker with anonymous methods ?

  1. #1
    Join Date
    Jun 2008
    Posts
    57

    BackgroundWorker with anonymous methods ?

    Hi,
    I'm gonna create a BackgroundWorker with an anonymous method.
    I've written the following code :

    PHP Code:
    BackgroundWorker bgw = new BackgroundWorker();
    bgw.DoWork += new DoWorkEventHandler(
        () =>
        {
            
    int i 0;
            foreach (var 
    item in query2)
            {
                ....
                ....
            }
        }
    ); 
    But Delegate 'System.ComponentModel.DoWorkEventHandler' does not take '0' arguments and I have to pass two objects to the anonymous method : object sender, DoWorkEventArgs e

    Could you please guide me, how I can do it ?
    Thanks.

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: BackgroundWorker with anonymous methods ?

    you should do exacly what the compiler is asking you to do:

    PHP Code:
    BackgroundWorker bw = new BackgroundWorker();
    bw.DoWork += delegate(Object senderDoWorkEventArgs e)
    {
        
    MessageBox.Show("Hi M-Dayyan!");
    };
    bw.RunWorkerAsync(); 
    there are hundrets of tutorials on anonymus methods and delegates. just google
    Last edited by memeloo; January 16th, 2010 at 12:49 PM.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  3. #3
    Join Date
    Jun 2008
    Posts
    57

    Re: BackgroundWorker with anonymous methods ?

    Thanks my friend,
    we could do that like this
    PHP Code:
    bgw.DoWork += new DoWorkEventHandler(
        (
    se1) =>
        {
            
    int i 0;
            foreach (var 
    item in query2)
            {
                ....
                ....
            }
        }
    ); 

  4. #4
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: BackgroundWorker with anonymous methods ?

    of course this time I chose the lighter method

    but even this is possible:
    PHP Code:
    BackgroundWorker bw = new BackgroundWorker();
    bw.DoWork += (sendere) =>
    {
        
    MessageBox.Show("Hi!");
    };
    bw.RunWorkerAsync(); 
    Last edited by memeloo; January 16th, 2010 at 12:47 PM.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center