24小时热门版块排行榜    

查看: 435  |  回复: 1

leafmavis

木虫 (文坛精英)

[求助] 请教线程数据传递问题

请教大家一个问题:用C++ builder 6.0编程,实现在一定的时长内反复播放两种声音(两声之间的时间间隔和播放总时长可变),而且可以随时暂停和继续。现在的程序能实现反复播放、暂停和继续,但是两声之间没有时间间隔,也即时间间隔的数据没有传递到线程中(播放时长还未编入到线程中),请大家帮忙看看,问题出在哪里?谢谢
附上4个文件(因还要实现其他功能,所以包含较多的头文件) 和图1个
1、Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#include <stdio.h>
#include <vcl.h>
#pragma hdrstop
#include <mmsystem.h>
#include "Unit1.h"
#include <stdlib.h>
#include <algorithm.h>
using namespace std;
FILE *outf;   //定义一个文件指针,用于随机顺序的输出
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------


TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
  outf=fopen("C:\\Documents and Settings\\Administrator\\桌面\\P300 play order.txt","w+";   //打开txt文件
  Button2->Enabled=false;
  Button3->Enabled=false;
  Button4->Enabled=true;

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  Button1->Enabled=false;
  Button2->Enabled=true;
  const float interval1=Edit1->Text.ToDouble();  //时间间隔
  int interval=interval1*1000; //the unit is ms
  const float duration1=Edit2->Text.ToDouble();  //播放总时长(分钟)
  int duration=duration1*60;   //the unit is s
  if(thread1==NULL)
    {
      thread1=new TMyThread(false,interval,duration);    //创建线程对象实例
    }
  thread1->Resume();    //单击后才启动线程
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button2Click(TObject *Sender)
{
   if(thread1!=NULL)  thread1->Suspend();
   Button2->Enabled=false;
   Button3->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
   thread1->Resume();
   Button2->Enabled=true;
   Button3->Enabled=false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)
{
   Form1->Close();
}
//---------------------------------------------------------------------------


2、Unit1.h
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "Unit2.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:        // IDE-managed Components
        TEdit *Edit1;
        TEdit *Edit2;
        TButton *Button1;
        TButton *Button2;
        TButton *Button3;
        TButton *Button4;
        TLabel *Label1;
        TLabel *Label2;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
        void __fastcall Button3Click(TObject *Sender);
        void __fastcall Button4Click(TObject *Sender);
private:        // User declarations
        TMyThread *thread1;
public:                // User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


3、Unit2.cpp
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
#include <stdlib.h>
#include <mmsystem.h>
#include <algorithm.h>
#pragma package(smart_init)
TDateTime startTime, currentTime;

//---------------------------------------------------------------------------

//   Important: Methods and properties of objects in VCL can only be
//   used in a method called using Synchronize, for example:
//
//      Synchronize(UpdateCaption);
//
//   where UpdateCaption could look like:
//
//      void __fastcall TMyThread::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//---------------------------------------------------------------------------

__fastcall TMyThread::TMyThread(bool CreateSuspended,int interIN, int duraIN)
        : TThread(CreateSuspended)
{  int inter = interIN;
   int duration = duraIN;
}

//---------------------------------------------------------------------------
void __fastcall TMyThread::Execute()
{
   //---- Place thread code here ----

    for(int i=0;i<200;i++)
    { PlaySound("inside.wav",NULL,SND_FILENAME|SND_SYNC);
      Sleep(inter);
      PlaySound("outside.wav",NULL,SND_FILENAME|SND_SYNC);
      Sleep(inter);
    }

}
//---------------------------------------------------------------------------


4、Unit2.h
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class TMyThread : public TThread
{            
private:


protected:
        void __fastcall Execute();

public:
        int inter,duration;
        __fastcall TMyThread(bool CreateSuspended, int interIN, int duraIN);    //注意:修改了默认参数);
};
//---------------------------------------------------------------------------
#endif

请教线程数据传递问题
form.JPG
回复此楼
春如杯中水
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

leafmavis

木虫 (文坛精英)

已解决,谢谢
春如杯中水
2楼2014-07-14 08:26:54
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 leafmavis 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +3 E4V9bALS54sj 2026-09-19 3/150 2026-09-20 01:39 by 9i7PCSZyMzot
[论文投稿] 售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急 +3 E4V9bALS54sj 2026-09-19 3/150 2026-09-20 01:27 by 9i7PCSZyMzot
[硕博家园] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +3 E4V9bALS54sj 2026-09-19 3/150 2026-09-20 01:27 by 9i7PCSZyMzot
[公派出国] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +3 E4V9bALS54sj 2026-09-19 3/150 2026-09-20 01:15 by 9i7PCSZyMzot
[博后之家] 售SCI一区T0P文章,我:8.O.55.1.O.5.4,科目全,可+急 +3 E4V9bALS54sj 2026-09-19 3/150 2026-09-20 01:15 by 9i7PCSZyMzot
[考博] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +3 E4V9bALS54sj 2026-09-19 3/150 2026-09-20 01:03 by 9i7PCSZyMzot
[论文投稿] 售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急 +3 E4V9bALS54sj 2026-09-19 3/150 2026-09-20 00:51 by 9i7PCSZyMzot
[考研] 售SCI一区文章,我:8O5.5.1.O5.4,科目全,可伽急 +3 4pRyVGCVerAA 2026-09-19 3/150 2026-09-20 00:15 by 9i7PCSZyMzot
[硕博家园] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +3 4pRyVGCVerAA 2026-09-19 3/150 2026-09-20 00:03 by 9i7PCSZyMzot
[找工作] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +4 4pRyVGCVerAA 2026-09-19 4/200 2026-09-20 00:03 by 9i7PCSZyMzot
[基金申请] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +3 4pRyVGCVerAA 2026-09-19 4/200 2026-09-19 23:39 by 9i7PCSZyMzot
[考研] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +3 4pRyVGCVerAA 2026-09-19 4/200 2026-09-19 23:27 by 9i7PCSZyMzot
[论文投稿] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +3 4pRyVGCVerAA 2026-09-19 4/200 2026-09-19 23:27 by 9i7PCSZyMzot
[考博] 售SCI一区T0P文章,我:8.O.55.1.O.54,科目齐全,可+急 +3 4pRyVGCVerAA 2026-09-19 4/200 2026-09-19 23:03 by 9i7PCSZyMzot
[公派出国] 售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急 +3 4pRyVGCVerAA 2026-09-18 7/350 2026-09-19 21:03 by 9i7PCSZyMzot
[教师之家] 现代”学阀”该如何界定 +3 iaeyuan 2026-09-19 3/150 2026-09-19 20:52 by 没昵称呀
[基金申请] 国社科系统bug了,是不是要放榜了? +7 kynobel 2026-09-16 9/450 2026-09-19 14:52 by 接好运婷
[考博] 申博发邮件 +8 Lmengk 2026-09-15 9/450 2026-09-19 09:27 by 中间体超市
[有机交流] 各位大神,目前国内有哪些比较好用的逆合成软件? 20+3 zapen 2026-09-15 9/450 2026-09-18 18:17 by zapen
[考博] 上海工程技术大学激光智能制造课题组招收博士研究生 +8 两三岁ss 2026-09-14 8/400 2026-09-16 09:46 by 水士口
信息提示
请填处理意见