24小时热门版块排行榜    

查看: 355  |  回复: 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 的主题更新
信息提示
请填处理意见