CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 1999
    Location
    G day Mayt Land
    Posts
    971

    another theread Q

    1-How do u figure out in teh main thread that teh worker thread is done its job ?
    2- not sure what exactly is teh purpose of Join ..

    what attribute /methdod needs to eb checked inorder to enable diable some button upon the worker threads completion.
    Code:
    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 ANZ_SHIELD_MIGBAU
    {
    
    
    
        public partial class Form1 : Form
        {
            Worker workerObject;
            Thread workerThread;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                btnProcess.Enabled = true;
                btnStop.Enabled = true;
            }
            private void btnProcess_Click(object sender, EventArgs e)
            {
                btnProcess.Enabled = false;
                btnStop.Enabled = true;
    
                textBox1.Text = "";
                workerObject = new Worker();
                workerThread = new Thread(workerObject.DoWork);
    
                workerThread.Start();
    
                while (!workerThread.IsAlive) ;
    
                Thread.Sleep(1);
                
    
                ?????????????????????
    
    
                
                // Use the Join method to block the current thread 
                // until the object's thread terminates.
          //      workerThread.Join();
    
                
    
            }
    
            private void btnStop_Click(object sender, EventArgs e)
            {
                // Request that the worker thread stop itself:
                workerObject.RequestStop();
                textBox1.Text += "process stopped" +workerObject.Get().ToString();
                btnProcess.Enabled = true;
                btnStop.Enabled = false;
            }
        }
    
        public class Worker
        {
            public void DoWork()
            {
                    _status = false;
                    for (int j = 0; j < 200; j++)
                    {
                        for (i = 0; i < 32000; i++)
                       {
                           if (_shouldStop)
                               break;
                        }
                        if (_shouldStop)
                           break;
                    }
                    if (_shouldStop)
                        MessageBox.Show("process was interrupted");
    
                    _status = true;
                   
    
            }
            public void RequestStop()
            {
                _shouldStop = true;
            }
            public int Get()
            {
                return i;
            }
            public bool  GetStatus()
            {
                return _shouldStop;
            }
    
            private volatile bool _shouldStop;
            public bool _status;
            private int i = 0;
        }
    
    
    }

  2. #2
    Join Date
    Apr 2006
    Posts
    220

    Re: another theread Q

    Your 'Thread' concepts seem weak. First solidify them before implementing them.

    P.S.
    'Thread.Join()' Blocks the calling thread until a thread terminates.
    http://msdn.microsoft.com/en-us/library/95hbf2ta.aspx

    Another helping link for starters
    http://www.cs.mtu.edu/~shene/NSF-3/e...anagement.html
    Last edited by nabeelisnabeel; April 15th, 2009 at 02:22 AM.

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

Featured