Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Can you run the same function from two backgroundWorkers

Can you run the same function from two backgroundWorkers

Scheduled Pinned Locked Moved C#
tutorialquestion
35 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M MacRaider4

    PIEBALDconsult wrote:

    Luc Pattyn wrote: lots of regexes

    here are the only lines

    Regex objLongDollar = new Regex("\\d+,\\d+\\.\\d+");
    Regex objNumRd = new Regex("\\d+rd");
    Regex objNumSt = new Regex("\\d+st");

    This is in the parse message section...

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #26

    The result will depend on how large the search object is, and how often you execute such regexes. When I care about performance, I avoid the Regex class. I use string methods, maybe a StringBuilder, maybe a character array, maybe several nested loops, but no regexes. Regexes are good for compact code when performance does not matter at all, and readability is not a primary concern either. Here[^] is the report on a little experiment I once performed. :)

    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

    1 Reply Last reply
    0
    • L Luc Pattyn

      MacRaider4 wrote:

      As you can see there is nothing different between them

      My sight tells otherwise. I suggest you start believing the error messages you are getting. :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      M Offline
      M Offline
      MacRaider4
      wrote on last edited by
      #27

      Luc Pattyn wrote:

      My sight tells otherwise. I suggest you start believing the error messages you are getting.

      Ok I've been staring at this for much too long, and have looked at it many times and I'm not seeing the difference other than one has a 2 and the other a 3. :~

      L 1 Reply Last reply
      0
      • P PIEBALDconsult

        That seems reasonable. I don't know anything about reading messages from a mail server, but... Each thread can read and parse one message and report back the result to be written. Whether or not the thread also downloads the message I don't know, but that should be doable. So you can have a class that distributes work to a bunch of threads. The process on the thread performs the work and reports back when finished. For writing, you can have an event handler that locks a stream when it writes.

        M Offline
        M Offline
        MacRaider4
        wrote on last edited by
        #28

        Ok so to speed up that section you are suggesting to create a class that passes work to lets say 4 background workers? That sounds really good, but I've never done anything like that and how would I then return that info back to the Form? With a return? I would also loose my updates on the progressBar would I not? Ok my brain is really starting to hurt now, thankfully I've only got 10 min left in my day right now... will have to get back to this tomorrow! Thank you all for everything thus far...

        P 1 Reply Last reply
        0
        • M MacRaider4

          Luc Pattyn wrote:

          My sight tells otherwise. I suggest you start believing the error messages you are getting.

          Ok I've been staring at this for much too long, and have looked at it many times and I'm not seeing the difference other than one has a 2 and the other a 3. :~

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #29

          one BaCKgroUNDworKEr isn't the other. :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          1 Reply Last reply
          0
          • M MacRaider4

            Ok so to speed up that section you are suggesting to create a class that passes work to lets say 4 background workers? That sounds really good, but I've never done anything like that and how would I then return that info back to the Form? With a return? I would also loose my updates on the progressBar would I not? Ok my brain is really starting to hurt now, thankfully I've only got 10 min left in my day right now... will have to get back to this tomorrow! Thank you all for everything thus far...

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #30

            MacRaider4 wrote:

            return that info back to the Form

            Well, I question the use of a form at all; I'd use a Windows Service, but that's just me. You can have a Service that pulls the data into the database and then the form pulls it (already fluffed and folded) from there. Or you could use an event.

            M 1 Reply Last reply
            0
            • P PIEBALDconsult

              MacRaider4 wrote:

              return that info back to the Form

              Well, I question the use of a form at all; I'd use a Windows Service, but that's just me. You can have a Service that pulls the data into the database and then the form pulls it (already fluffed and folded) from there. Or you could use an event.

              M Offline
              M Offline
              MacRaider4
              wrote on last edited by
              #31

              I'll have to look up services as I've never done anything with that before. Though I will say doing this project has made me a better C# programmer, at this rate in another year I'll be answering some of these questions for other people. Some one else mentioned just doing this all in one pass, now that I'm looking back at my code I think that is a very good idea. Is this something I could do with the service or event? I could then do my initial pass to get the number, then have a couple workers work on the list storing the data in arrays. Once those are done combine the arrays or better yet just have the arrays loaded stright into the database which should take no time at all even with checking to make sure that message isn't already there?

              P 1 Reply Last reply
              0
              • M MacRaider4

                I'll have to look up services as I've never done anything with that before. Though I will say doing this project has made me a better C# programmer, at this rate in another year I'll be answering some of these questions for other people. Some one else mentioned just doing this all in one pass, now that I'm looking back at my code I think that is a very good idea. Is this something I could do with the service or event? I could then do my initial pass to get the number, then have a couple workers work on the list storing the data in arrays. Once those are done combine the arrays or better yet just have the arrays loaded stright into the database which should take no time at all even with checking to make sure that message isn't already there?

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #32

                MacRaider4 wrote:

                have the arrays loaded stright into the database

                Right. The Service would periodically (once a minute?) query the email server for messages, if there are some, get them, process them, and stick the results in the database. You could still use a thread to process each message in necessary. Depending on your needs, you could then have the same Service host a WCF Web Service that your client application can use to get the data. :thumbsup:

                M 1 Reply Last reply
                0
                • P PIEBALDconsult

                  MacRaider4 wrote:

                  have the arrays loaded stright into the database

                  Right. The Service would periodically (once a minute?) query the email server for messages, if there are some, get them, process them, and stick the results in the database. You could still use a thread to process each message in necessary. Depending on your needs, you could then have the same Service host a WCF Web Service that your client application can use to get the data. :thumbsup:

                  M Offline
                  M Offline
                  MacRaider4
                  wrote on last edited by
                  #33

                  That was my original intent once I got it working, just didn't know about the service part. So let me see if I have this right now: 1. Log into the server and get the number of messages 2. Decide if I need to use a bgw and how many 3. Do the work with no or a couple workers: a. have the worker/s log in with the number of account/s each is processing b. process the "entire" message all at once and store in an array 4. Update the database 5. Have the form check fo updates? Do this sound about right?

                  P 1 Reply Last reply
                  0
                  • M MacRaider4

                    That was my original intent once I got it working, just didn't know about the service part. So let me see if I have this right now: 1. Log into the server and get the number of messages 2. Decide if I need to use a bgw and how many 3. Do the work with no or a couple workers: a. have the worker/s log in with the number of account/s each is processing b. process the "entire" message all at once and store in an array 4. Update the database 5. Have the form check fo updates? Do this sound about right?

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #34

                    Yeah, basically. But remember that I'm not familiar with reading messages from an email server, so I don't understand the "a. have the worker/s log in with the number of account/s each is processing" part. I would have a worker read a message, process it, and stick it in the database; then maybe get another. Or read all the available messages and pass them to the workers. There are many ways to skin this cat.

                    M 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Yeah, basically. But remember that I'm not familiar with reading messages from an email server, so I don't understand the "a. have the worker/s log in with the number of account/s each is processing" part. I would have a worker read a message, process it, and stick it in the database; then maybe get another. Or read all the available messages and pass them to the workers. There are many ways to skin this cat.

                      M Offline
                      M Offline
                      MacRaider4
                      wrote on last edited by
                      #35

                      Still can't get my 3rd worker to "work" as I'm still getting the backgroundWorker3 does not exist in the current context in the first occurance of each line in InitializeBackgroundWorker3. So that's putting a hinderance on everything.

                      public Form1()
                      {
                      InitializeComponent();
                      InitializeBackgroundWorker();
                      InitializeBackgroundWorker2();
                      InitializeBackgroundWorker3();

                              btnGetMessageInfo.Enabled = false;
                              btnCancelConnection.Enabled = false;
                      
                          }
                          private void InitializeBackgroundWorker()
                          {
                              backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1\_DoWork);
                              backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1\_RunWorkerCompleted);
                              backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker3\_ProgressChanged);
                          }
                      
                          private void InitializeBackgroundWorker2()
                          {
                              backgroundWorker2.DoWork += new DoWorkEventHandler(backgroundWorker2\_DoWork);
                              backgroundWorker2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker2\_RunWorkerCompleted);
                              backgroundWorker2.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker2\_ProgressChanged);
                          }
                      
                          private void InitializeBackgroundWorker3()
                          {
                              backgroundWorker3.DoWork += new DoWorkEventHandler(backgroundWorker3\_DoWork);
                              backgroundWorker3.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker3\_RunWorkerCompleted);
                              backgroundWorker3.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker3\_ProgressChanged);
                          }
                      

                      So what I have now is when you click on the connect button (first thing you can do), it logs into the server and gets the total number and size of each message. And stores that in a global variable and displays some info on the form. Then it figures out how many workers to use (based on number of messages)and assigns start and end values for each worker. I'm now in the process of writing the work for the workers (focusing on 1 and 2 since only those work). It's moving along, though slowly.

                      modified on Friday, February 11, 2011 1:33 PM

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups