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. C# Threading

C# Threading

Scheduled Pinned Locked Moved C#
tutorialcsharpquestion
7 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.
  • B Offline
    B Offline
    budidharma
    wrote on last edited by
    #1

    I've never worked with multithreading before in any language. I'm trying to figure out how to do the following: I have a function, which generates unique values based on the current time. It saves the data to a log file. I have it set that when I click a button, it executes this code. Rather, what I need to happen is to have it continually execute the function every 60 seconds until I reclick the same button. I assume I need to use threads to do this. I've looked up several tutorials, but I really can't find an example to do this. Can someone give me an idea? Thanks.

    B 1 Reply Last reply
    0
    • B budidharma

      I've never worked with multithreading before in any language. I'm trying to figure out how to do the following: I have a function, which generates unique values based on the current time. It saves the data to a log file. I have it set that when I click a button, it executes this code. Rather, what I need to happen is to have it continually execute the function every 60 seconds until I reclick the same button. I assume I need to use threads to do this. I've looked up several tutorials, but I really can't find an example to do this. Can someone give me an idea? Thanks.

      B Offline
      B Offline
      budidharma
      wrote on last edited by
      #2

      ... this is the function I have. The first if clause needs to start a thread which executes the SaveWindow function every 60 seconds. The second needs to terminate the thread. I don't even know if this is the way threads work. private void buttonScraperControl_Click(object sender, EventArgs e) { if (buttonScraperControl.Text == "Start") { buttonScraperControl.Text = "Stop"; SaveWindow(); // EXECUTE THIS FUNCTION EVERY 60 SECONDS } else if (buttonScraperControl.Text == "Stop") { buttonScraperControl.Text = "Start"; // STOP FROM EXECUTING EVERY 60 SECONDS } }

      H 1 Reply Last reply
      0
      • B budidharma

        ... this is the function I have. The first if clause needs to start a thread which executes the SaveWindow function every 60 seconds. The second needs to terminate the thread. I don't even know if this is the way threads work. private void buttonScraperControl_Click(object sender, EventArgs e) { if (buttonScraperControl.Text == "Start") { buttonScraperControl.Text = "Stop"; SaveWindow(); // EXECUTE THIS FUNCTION EVERY 60 SECONDS } else if (buttonScraperControl.Text == "Stop") { buttonScraperControl.Text = "Start"; // STOP FROM EXECUTING EVERY 60 SECONDS } }

        H Offline
        H Offline
        Heks
        wrote on last edited by
        #3

        I believe you can use the built in "Timer" control. http://www.c-sharpcorner.com/controls/timer.asp

        B 1 Reply Last reply
        0
        • H Heks

          I believe you can use the built in "Timer" control. http://www.c-sharpcorner.com/controls/timer.asp

          B Offline
          B Offline
          budidharma
          wrote on last edited by
          #4

          Will the timer stop execution of other parts of the program? There are several other things that the user will be doing while this bit of code is executing. Is a timer really the correct solution? Thanks!

          G E S 3 Replies Last reply
          0
          • B budidharma

            Will the timer stop execution of other parts of the program? There are several other things that the user will be doing while this bit of code is executing. Is a timer really the correct solution? Thanks!

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            If you use a timer, it will be using a single thread. That means that the timer will only be triggered when your program is not busy doing anything else. If you use a separate thread, it will be running even if your program is busy. The drawback is that the code has to be thread safe, meaning that if you are using any variables from both threads, you will have to take special precausions to make sure that only one thread at a time is using the variables. --- b { font-weight: normal; }

            1 Reply Last reply
            0
            • B budidharma

              Will the timer stop execution of other parts of the program? There are several other things that the user will be doing while this bit of code is executing. Is a timer really the correct solution? Thanks!

              E Offline
              E Offline
              eggie5
              wrote on last edited by
              #6

              The System.Form.Timer or whatever it is, for some reason deson't lock up your GUI. Someone once explained why to me... /\ |_ E X E GG

              1 Reply Last reply
              0
              • B budidharma

                Will the timer stop execution of other parts of the program? There are several other things that the user will be doing while this bit of code is executing. Is a timer really the correct solution? Thanks!

                S Offline
                S Offline
                Stefan Troschuetz
                wrote on last edited by
                #7

                I think a timer would be a good solution for your problem, cause it was made to execute code in regular intervals. Now you have to select the right timer for your needs. If you use the System.Windows.Forms.Timer, your code will be executed in the UI thread. This prevents you from paying attention to threading issues, but if the code is time-consuming it will hang your GUI, cause the UI thread is busy. If you use System.Timers.Timer or the System.Threading.Timer, your code will be executed by a worker thread. This won't affect your GUI, but you have to pay attention to threading issues e.g. change of GUI controls. Take a look at this site[^] on MSDN for some more information.


                www.troschuetz.de

                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