给位高手,我刚用MFC基于对话框弄个多个进度条同时运行,但不知如何同时启动他们和同时让他们暂停,请高手指点,我知道是要创建线程和挂起,具体不会编创建和启动

解决方案 »

  1.   

    看看这个
    http://download.csdn.net/source/2516354
      

  2.   

    ResumeThread(hThread);
    SuspendThread(hThread);
      

  3.   

    oyljerry,哥能给一段代码说明不?
    我想用一个开始按钮运行线程,用暂停将线程挂起,有代码可以参考么
      

  4.   

    大哥们帮忙看我的代码那错了 ,我想用一个开始按钮运行线程,用暂停按钮将线程挂起void CEreDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
        m_Prog1.SetRange(1,20000);   //设置滑块的滑动范围
    m_Prog2.SetRange(1,20000);   //设置滑块的滑动范围
    m_Prog3.SetRange(1,20000);   //设置滑块的滑动范围

    HANDLE hThread1 = CreateThread(NULL,0,ThreadProc1,this,CREATE_SUSPENDED,NULL); //创建线程,并挂起CREATE_SUSPENDED
    SetThreadPriority(hThread1,THREAD_PRIORITY_LOWEST);    //设置优先级 HANDLE hThread2 = CreateThread(NULL,0,ThreadProc2,this,CREATE_SUSPENDED,NULL); //创建线程,并挂起CREATE_SUSPENDED
    SetThreadPriority(hThread2,THREAD_PRIORITY_BELOW_NORMAL);  //设置优先级 HANDLE hThread3 = CreateThread(NULL,0,ThreadProc3,this,CREATE_SUSPENDED,NULL); //创建线程,并挂起CREATE_SUSPENDED
    SetThreadPriority(hThread3,THREAD_PRIORITY_HIGHEST);     //设置优先级
    ResumeThread(hThread1);  //恢复线程1
    ResumeThread(hThread2);  //恢复线程2
    ResumeThread(hThread3);  //恢复线程3

    }void CEreDlg::OnButton2() 
    {
    // TODO: Add your control notification handler code here
           SuspendThread( hThread1);  //挂起线程1
            SuspendThread( hThread2);  //挂起线程2
            SuspendThread( hThread3);  //挂起线程3

    }
      

  5.   

    程序提示我下面出错
    F:\学习\新建文件夹 (2)\ere\ereDlg.cpp(229) : error C2065: 'hThread1' : undeclared identifier
    F:\学习\新建文件夹 (2)\ere\ereDlg.cpp(230) : error C2065: 'hThread2' : undeclared identifier
    F:\学习\新建文件夹 (2)\ere\ereDlg.cpp(231) : error C2065: 'hThread3' : undeclared identifier
    执行 cl.exe 时出错.
      

  6.   

    Event可以.临界区/mutex 也能挂。但控制比较麻烦。要其他线程先lock变量也行。关键是这变量要自己控制好。别多个线程操作这变量就行
      

  7.   

    文件夹 (2)\ere\ereDlg.cpp(229) : error C2065: 'hThread1' : undeclared identifier解决:HANDLE hThread1要么作为CEreDlg的成员要么放在外面做全局变量HANDLE hThread1;//全局变量void CEreDlg::OnButton1()  
    {...xxx
      

  8.   


    关于线程的案例请发我   谢谢!!!  [email protected]
      

  9.   

    class CAboutDlg : public CDialog
    {
    public:
    CAboutDlg();// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };
    HANDLE hThread1;
    HANDLE hThread2;
    HANDLE hThread3;CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
    }void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
    // No message handlers
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CEreDlg dialogCEreDlg::CEreDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CEreDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CEreDlg)
    // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }void CEreDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CEreDlg)
    DDX_Control(pDX, IDC_PROGRESS3, m_Prog3);
    DDX_Control(pDX, IDC_PROGRESS2, m_Prog2);
    DDX_Control(pDX, IDC_PROGRESS1, m_Prog1);
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CEreDlg, CDialog)
    //{{AFX_MSG_MAP(CEreDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
    ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CEreDlg message handlersBOOL CEreDlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here

    return TRUE;  // return TRUE  unless you set the focus to a control
    }void CEreDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
    }
    else
    {
    CDialog::OnSysCommand(nID, lParam);
    }
    }// If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.void CEreDlg::OnPaint() 
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    CDialog::OnPaint();
    }
    }// The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CEreDlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }
    DWORD __stdcall ThreadProc1(LPVOID lpParameter)
    {
    CEreDlg* pDlg = (CEreDlg*)lpParameter;
    for (int i=0; i<=20000; i++)
    {
    pDlg->m_Prog1.SetPos(i);
    Sleep(2);
    }
    return 0;
    }
    DWORD __stdcall ThreadProc2(LPVOID lpParameter)
    {
    CEreDlg* pDlg = (CEreDlg*)lpParameter;
    for (int i=0; i<=20000; i++)
    {
    pDlg->m_Prog2.SetPos(i);
    Sleep(2);
    }
    return 0;
    }
    DWORD __stdcall ThreadProc3(LPVOID lpParameter)
    {
    CEreDlg* pDlg = (CEreDlg*)lpParameter;
    for (int i=0; i<=20000; i++)
    {
    pDlg->m_Prog3.SetPos(i);
    Sleep(2);
    }
    return 0;
    }
    void CEreDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
        m_Prog1.SetRange(1,20000);   //设置滑块的滑动范围
    m_Prog2.SetRange(1,20000);   //设置滑块的滑动范围
    m_Prog3.SetRange(1,20000);   //设置滑块的滑动范围

    HANDLE hThread1 = CreateThread(NULL,0,ThreadProc1,this,CREATE_SUSPENDED,NULL); //创建线程,并挂起CREATE_SUSPENDED
    SetThreadPriority(hThread1,THREAD_PRIORITY_LOWEST);    //设置优先级 HANDLE hThread2 = CreateThread(NULL,0,ThreadProc2,this,CREATE_SUSPENDED,NULL); //创建线程,并挂起CREATE_SUSPENDED
    SetThreadPriority(hThread2,THREAD_PRIORITY_BELOW_NORMAL);  //设置优先级 HANDLE hThread3 = CreateThread(NULL,0,ThreadProc3,this,CREATE_SUSPENDED,NULL); //创建线程,并挂起CREATE_SUSPENDED
    SetThreadPriority(hThread3,THREAD_PRIORITY_HIGHEST);     //设置优先级
    ResumeThread(hThread1);  //恢复线程
    ResumeThread(hThread2);  //恢复线程
    ResumeThread(hThread3);  //恢复线程

    }void CEreDlg::OnButton2() 
    {
    // TODO: Add your control notification handler code here
    SuspendThread( hThread1);  //挂起线程
            SuspendThread( hThread2);  //挂起线程
            SuspendThread( hThread3);  //挂起线程
      

  10.   

    Sleep(2);  是说要放弃2毫秒。。 起码要Sleep(100);嘛Sleep(2);几一下就跑完了你那个循环。线程就退出了。