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 / C++ / MFC
  4. Threading

Threading

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
89 Posts 23 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.
  • S sujithkumarsl

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace testApp { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Thread newThread; private void btnOK_Click(object sender, EventArgs e) { newThread = new Thread(new ThreadStart(Display)); newThread.IsBackground = true; newThread.Start(); } private void Display() { MessageBox.Show("threadStarted"); } private void btnCancel_Click(object sender, EventArgs e) { newThread.Abort(); MessageBox.Show("threadstopped"); } } }

    My small attempt...

    C Offline
    C Offline
    Cedric Moonen
    wrote on last edited by
    #4

    This is managed code, we are on the C++ forum.


    Cédric Moonen Software developer
    Charting control [v1.2]

    1 Reply Last reply
    0
    • S sujithkumarsl

      using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace testApp { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Thread newThread; private void btnOK_Click(object sender, EventArgs e) { newThread = new Thread(new ThreadStart(Display)); newThread.IsBackground = true; newThread.Start(); } private void Display() { MessageBox.Show("threadStarted"); } private void btnCancel_Click(object sender, EventArgs e) { newThread.Abort(); MessageBox.Show("threadstopped"); } } }

      My small attempt...

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #5

      Your code needs to a convertor to convert of managed code to unmanaged code :-D


      WhiteSky


      A 1 Reply Last reply
      0
      • D deeps_cute

        hi i have to run a thread when a button say ok is clicked. if the user press cancel threading should be exited. should not close the dialog. how to do this.

        Arise Awake Stop Not Till ur Goal is Reached.

        B Offline
        B Offline
        BarnaKol
        wrote on last edited by
        #6

        Hi, Keep two buttons. One OK and another Cancel on the dialog. Keep a member variable in the dialog class of type CWinThread* say m_pThread. Now create the thread in the OnOK() event handler as m_pThread=AfxBeginThread(ThFunc,0,0); where ThFunc is the function pointer to your thread controlling function and the next argument is the argument passed to your function which I have passed 0 here. This function should be of type UINT YourThreadFunc(LPVOID pParam). To stop execution of the thread write in the OnCancel() event handler m_pThread->SuspendThread(); Hope this will help you. Barna

        S 1 Reply Last reply
        0
        • B BarnaKol

          Hi, Keep two buttons. One OK and another Cancel on the dialog. Keep a member variable in the dialog class of type CWinThread* say m_pThread. Now create the thread in the OnOK() event handler as m_pThread=AfxBeginThread(ThFunc,0,0); where ThFunc is the function pointer to your thread controlling function and the next argument is the argument passed to your function which I have passed 0 here. This function should be of type UINT YourThreadFunc(LPVOID pParam). To stop execution of the thread write in the OnCancel() event handler m_pThread->SuspendThread(); Hope this will help you. Barna

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #7

          Bad advice. See here[^] for details.

          Steve

          B 1 Reply Last reply
          0
          • D deeps_cute

            hi i have to run a thread when a button say ok is clicked. if the user press cancel threading should be exited. should not close the dialog. how to do this.

            Arise Awake Stop Not Till ur Goal is Reached.

            R Offline
            R Offline
            Russell
            wrote on last edited by
            #8

            Here you can find a lot a information about threads: http://www.codeproject.com/threads/usingworkerthreads.asp[^] of course,...also the way to braek it in simple way.

            Cheers, Russell

            S S 2 Replies Last reply
            0
            • S Stephen Hewitt

              Bad advice. See here[^] for details.

              Steve

              B Offline
              B Offline
              BarnaKol
              wrote on last edited by
              #9

              If you can come up with some good advice and a real solution to the actual problem then put it there. Barna

              S R B 3 Replies Last reply
              0
              • B BarnaKol

                If you can come up with some good advice and a real solution to the actual problem then put it there. Barna

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #10

                My advice was valuable: pointing out unsafe solutions is a valid point to make.

                Steve

                B 1 Reply Last reply
                0
                • B BarnaKol

                  If you can come up with some good advice and a real solution to the actual problem then put it there. Barna

                  R Offline
                  R Offline
                  Rajesh R Subramanian
                  wrote on last edited by
                  #11

                  It is better not to give any advice than giving bad advice. Is it too difficult for you to understand this?

                  "The difficulty lies, not in the new ideas, but in escaping from the old ones." -- John Maynard Keyes, 1936

                  B T 3 Replies Last reply
                  0
                  • B BarnaKol

                    If you can come up with some good advice and a real solution to the actual problem then put it there. Barna

                    B Offline
                    B Offline
                    BadKarma
                    wrote on last edited by
                    #12

                    Considering this is a worker thread I should do a single function for example convert some data it could use some resources. If you suspend the thread these resourse are still locked. So thats dangerous. You could change your design to use a state engine in a for(;;) loop breaking the whole functionality in small routines between each routine call you could check whether the thread should be closed. If you should stop the thread it can clear all the allocated resources and stuff. To signal if the thread should be stopped you can use synchronization objects. CEvent for instance. Even simpler can be to use the functions InterlockedIncrement which increment a long variable in a threadsafe manner.

                    codito ergo sum

                    1 Reply Last reply
                    0
                    • R Russell

                      Here you can find a lot a information about threads: http://www.codeproject.com/threads/usingworkerthreads.asp[^] of course,...also the way to braek it in simple way.

                      Cheers, Russell

                      S Offline
                      S Offline
                      sujithkumarsl
                      wrote on last edited by
                      #13

                      sorry. actually i thought its the c# forum

                      My small attempt...

                      T 1 Reply Last reply
                      0
                      • S Stephen Hewitt

                        My advice was valuable: pointing out unsafe solutions is a valid point to make.

                        Steve

                        B Offline
                        B Offline
                        BarnaKol
                        wrote on last edited by
                        #14

                        The point is to get a solution to the actual problem posted. Do that. Barna

                        S 1 Reply Last reply
                        0
                        • R Rajesh R Subramanian

                          It is better not to give any advice than giving bad advice. Is it too difficult for you to understand this?

                          "The difficulty lies, not in the new ideas, but in escaping from the old ones." -- John Maynard Keyes, 1936

                          B Offline
                          B Offline
                          BarnaKol
                          wrote on last edited by
                          #15

                          It is always better to concentrate on the problem posted than comparing ideas.Is it too difficult for you to understand this? Barna

                          R 1 Reply Last reply
                          0
                          • B BarnaKol

                            The point is to get a solution to the actual problem posted. Do that. Barna

                            S Offline
                            S Offline
                            Stephen Hewitt
                            wrote on last edited by
                            #16

                            Look dude, I just pointed out that your solution was bad. This took me 5 seconds and one line of text. I post heaps of messages on this forum and make an effort to be helpful – when time permits. Now I’m sorry if you’ve been offended but nevertheless my post has merit and is informative. Perhaps not as immediately useful as a full solution: but knowing what not to do should not be underrated. Perhaps it’s taught you something for example.

                            Steve

                            R B 2 Replies Last reply
                            0
                            • B BarnaKol

                              It is always better to concentrate on the problem posted than comparing ideas.Is it too difficult for you to understand this? Barna

                              R Offline
                              R Offline
                              Rajesh R Subramanian
                              wrote on last edited by
                              #17

                              BarnaKol wrote:

                              It is always better to concentrate on the problem posted than comparing ideas

                              Agreed. At the same time, when someone is giving a bad advice, we can point that out. Any sane person would realise his mistake, instead of arguing after giving wrong advice on a public forum.

                              "The difficulty lies, not in the new ideas, but in escaping from the old ones." -- John Maynard Keyes, 1936

                              B 1 Reply Last reply
                              0
                              • S Stephen Hewitt

                                Look dude, I just pointed out that your solution was bad. This took me 5 seconds and one line of text. I post heaps of messages on this forum and make an effort to be helpful – when time permits. Now I’m sorry if you’ve been offended but nevertheless my post has merit and is informative. Perhaps not as immediately useful as a full solution: but knowing what not to do should not be underrated. Perhaps it’s taught you something for example.

                                Steve

                                R Offline
                                R Offline
                                Rajesh R Subramanian
                                wrote on last edited by
                                #18

                                Steve, please stop arguing with him. Any person with a civic sense must have understood that what he did was a mistake by now. Ironically, he keeps arguing.

                                "The difficulty lies, not in the new ideas, but in escaping from the old ones." -- John Maynard Keyes, 1936

                                B D T 3 Replies Last reply
                                0
                                • S Stephen Hewitt

                                  Look dude, I just pointed out that your solution was bad. This took me 5 seconds and one line of text. I post heaps of messages on this forum and make an effort to be helpful – when time permits. Now I’m sorry if you’ve been offended but nevertheless my post has merit and is informative. Perhaps not as immediately useful as a full solution: but knowing what not to do should not be underrated. Perhaps it’s taught you something for example.

                                  Steve

                                  B Offline
                                  B Offline
                                  BarnaKol
                                  wrote on last edited by
                                  #19

                                  Look dude, What is of use to know how long it took you to understand and how many lines you wrote. What is of use to know how big heaps of messages you post in this forum.You dont have the actual solution lacks your merit. Why "Perhaps not as immediately useful as a full solution". Why not the full solution. My posts have also taught you how to post messages. Barna

                                  S 1 Reply Last reply
                                  0
                                  • R Rajesh R Subramanian

                                    Steve, please stop arguing with him. Any person with a civic sense must have understood that what he did was a mistake by now. Ironically, he keeps arguing.

                                    "The difficulty lies, not in the new ideas, but in escaping from the old ones." -- John Maynard Keyes, 1936

                                    B Offline
                                    B Offline
                                    BarnaKol
                                    wrote on last edited by
                                    #20

                                    There is no point in any argument with persons or persons who does not know how to write a line of text. Any person with a civic sense moral attitude and good characteristics must have understood that till now.

                                    R 1 Reply Last reply
                                    0
                                    • B BarnaKol

                                      Look dude, What is of use to know how long it took you to understand and how many lines you wrote. What is of use to know how big heaps of messages you post in this forum.You dont have the actual solution lacks your merit. Why "Perhaps not as immediately useful as a full solution". Why not the full solution. My posts have also taught you how to post messages. Barna

                                      S Offline
                                      S Offline
                                      Stephen Hewitt
                                      wrote on last edited by
                                      #21

                                      I'm going to take brahmma's advice and stop arguing this issue after this post. But first I'll say this:  Your advice was bad, pure and simple. In fact it was very bad as it creates a class of bug that's very hard to track down and is nondeterministic. Now again I reiterate that I’m sorry if you got offended or embarrassed, but I’ve taught both you and potentially the OP a valuable lesson. I have spent as much time on this issue as I can afford to, I’m sorry if this isn’t good enough for you. There is no law that says I can only respond to the OP: the threads are hierarchical after all.

                                      Steve

                                      B 1 Reply Last reply
                                      0
                                      • R Rajesh R Subramanian

                                        It is better not to give any advice than giving bad advice. Is it too difficult for you to understand this?

                                        "The difficulty lies, not in the new ideas, but in escaping from the old ones." -- John Maynard Keyes, 1936

                                        B Offline
                                        B Offline
                                        BarnaKol
                                        wrote on last edited by
                                        #22

                                        without guggling on the ideas come with a solution to the actual problem posted.

                                        1 Reply Last reply
                                        0
                                        • R Russell

                                          Here you can find a lot a information about threads: http://www.codeproject.com/threads/usingworkerthreads.asp[^] of course,...also the way to braek it in simple way.

                                          Cheers, Russell

                                          S Offline
                                          S Offline
                                          Sam_c
                                          wrote on last edited by
                                          #23

                                          Yeah I think all your answers and examples are going to be in that article. I hadn’t see that article before and I’m just going to read through it now and it looks pretty good. a little tip bit. 1) make sure that if you pause or stop a thread that you handle memory allocation and de-allocation. 2) try to avoid methods such as global variables to control threads and use of sleep() in a GUI based thread or thread that takes messages. Good looking article and well worth the read thanks for the link :)

                                          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