请教线程数据传递问题

leafmavis 2014-07-13 05:15:19
请教大家一个问题:用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
...全文
87 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
leafmavis 2014-07-14
  • 打赏
  • 举报
回复
确实如此,谢谢
dataxdata 2014-07-14
  • 打赏
  • 举报
回复
关键是要把int去掉,改成 inter = interN; 加上int就变成给新定义的同名自动变量赋值,而不是赋给成员变量
leafmavis 2014-07-13
  • 打赏
  • 举报
回复
将 int inter = interIN; 改为 int m_inter = interIN; 现象还是一样的,提示:[C++ Warning] Unit2.cpp(30): W8004 'm_inter' is assigned a value that is never used
dataxdata 2014-07-13
  • 打赏
  • 举报
回复
线程构造函数中的这句: int inter = interIN; 数值赋给了一个同名的自动变量,没有赋给成员变量 最好养成好习惯成员变量加上m_前缀

13,822

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧