24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 387  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 材料与化工306分找调剂 +9 沧海轻舟e 2026-04-02 9/450 2026-04-02 17:29 by oooqiao
[考研] 一志愿北京科技材料科学与工程288分,求调剂 +13 是辰啊 2026-04-02 13/650 2026-04-02 17:07 by 我是小康
[考研] 348求调剂 +11 zzzzyk123 2026-04-01 11/550 2026-04-02 16:52 by Wang200018
[考研] 085801 总分275 本科新能源 求调剂 +16 bradoner 2026-04-01 20/1000 2026-04-02 15:27 by BruceLiu320
[考研] 309求调剂 +8 呆菇不是戴夫 2026-04-02 8/400 2026-04-02 14:30 by oooqiao
[考研] 283求调剂 +3 jiouuu 2026-04-02 4/200 2026-04-02 14:08 by 哒哒哒呱呱呱
[考研] 生医工0831调剂求推荐 +5 小熊睿睿_s 2026-03-27 7/350 2026-04-02 11:30 by 纺大杨老师
[考研] 一志愿南师大0703化学 275求调剂 +6 Ripcord上岸 2026-03-27 6/300 2026-04-02 11:19 by TTTpp
[考研] 377求调剂 +3 RASKIN 2026-04-02 3/150 2026-04-02 09:45 by zzchen2000
[考研] 274求调剂 +8 薛定谔的虎。 2026-04-01 8/400 2026-04-02 09:21 by 晴空210210
[考研] 调剂 +3 好好读书。 2026-04-01 3/150 2026-04-01 17:06 by zhouyuwinner
[考研] 358求调剂 +3 王向阳花 2026-03-31 3/150 2026-04-01 09:56 by zzchen2000
[考研] 一志愿武理材料工程302调剂环化或化工 +15 Doleres 2026-03-31 16/800 2026-04-01 09:49 by lfj11
[考研] 263求调剂 +3 DDDDuu 2026-03-27 3/150 2026-03-31 16:21 by 土木硕士招生
[考研] 调剂求院校招收 +7 鹤鲸鸽 2026-03-28 7/350 2026-03-31 11:21 by oooqiao
[考研] 323分 食品与营养调剂 +3 嘿ooo 2026-03-31 3/150 2026-03-31 09:38 by longlotian
[考研] 求调剂 +10 家佳佳佳佳佳 2026-03-29 10/500 2026-03-30 18:34 by 544594351
[考研] 一志愿南开大学0710生物学359求调剂 +5 兔兔兔111223314 2026-03-29 7/350 2026-03-30 18:29 by 兔兔兔111223314
[考研] 085405 考的11408求各位老师带走 +3 Qiu学ing 2026-03-28 3/150 2026-03-28 09:19 by 乐呵呵的追梦人
[考研] 070300化学求调剂 +4 起个名咋这么难 2026-03-27 4/200 2026-03-27 21:39 by 83503孙老师
信息提示
请填处理意见