Results 1 to 8 of 8

Thread: Backgroundworker in WPF?

Hybrid View

  1. #1

    Thread Starter
    Lively Member Zolomon's Avatar
    Join Date
    Oct 2007
    Location
    Sweden
    Posts
    104

    Backgroundworker in WPF?

    Hello!

    How do I create and use a backgroundworker in WPF, C#?

    Been googlin' around for ages without any luck.
    //Zolomon

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Backgroundworker in WPF?

    Im guessing you are like me and are used to just dragging and dropping one in from the toolbox? I was a bit stumped when I first started to use WPF but dont worry its pretty easy to do still (and this is apparently the way that a lot of people do it in winforms anyway).

    All you need to do is declare it yourself at class level and if you use the WithEvents keyword then you dont need to hook up the event handlers yourself or anything.

    So for example, at class level (ie not in a sub or function) :
    vb.net Code:
    1. Private WithEvents bgworker As New ComponentModel.BackgroundWorker

    Then you can just use the drop down list at the top of the code editor to select "bgworker" (as I have named it, obviously you can give it a different name if necessary) and then select the DoWork event or whichever event you want to create a handler for from the drop down list next to it.
    Alternatively you can type out the event handlers manually, so for example:
    vb.net Code:
    1. Private Sub bgworker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgworker.DoWork
    2.  
    3. End Sub

    That help?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Lively Member Zolomon's Avatar
    Join Date
    Oct 2007
    Location
    Sweden
    Posts
    104

    Re: Backgroundworker in WPF?

    A little, but I don't think the drop-down thingie works for C#.

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Backgroundworker in WPF?

    Ah sorry I havent tried it in C# but I assumed it would be the same... one sec and I'll give it a go
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Backgroundworker in WPF?

    OK bear in mind that I have never touched C# before (seen plenty of examples though thanks to MS not providing VB documentation for many things...) so this might be rubbish but I think it should go something like this:

    At the top of your code:
    c#.net Code:
    1. using System.ComponentModel
    At class level:
    c#.net Code:
    1. BackgroundWorker bgworker = new BackgroundWorker();
    In your window_load event or wherever suits, hook up the event handlers:
    c#.net Code:
    1. bgworker.DoWork += new DoWorkEventHandler(bgworker_DoWork);
    2. bgworker.RunWorkerCompleted +=new RunWorkerCompletedEventHandler(bgworker_RunWorkerCompleted);
    Define the event handlers:
    c#.net Code:
    1. private void bgworker_DoWork(object sender,DoWorkEventArgs e)
    2.         {
    3.            //Do background worker stuff here
    4.         }
    c#.net Code:
    1. private void bgworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    2.         {
    3.             //Background worker has finished
    4.          }

    Any use?
    Last edited by chris128; Jul 26th, 2009 at 05:44 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6

    Thread Starter
    Lively Member Zolomon's Avatar
    Join Date
    Oct 2007
    Location
    Sweden
    Posts
    104

    Re: Backgroundworker in WPF?

    Thanks! Working like a charm!

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Backgroundworker in WPF?

    no worries, dont forget to mark the thread as resolved
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Backgroundworker in WPF?

    The C# IDE will help you create event handlers too, although in quite a different way to VB. You don't have to write out the event handler yourself. You just use the Tab key at the opportune moments to have the IDE generate the code for you. You may already have been aware but in case you weren't, and for others, you just type the "+=" and then hit Tab twice to insert the skeleton code.
    Attached Images Attached Images   

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