// FileCopyDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FileCopy.h"
#include "FileCopyDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
};
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()
/////////////////////////////////////////////////////////////
//
// 以下为多线程应用
//
/////////////////////////////////////////////////////////////
char Buf1[BUFSIZ];//第一个内存区
char Buf2[BUFSIZ];//第二个内存区
int Buf1Size, Buf2Size;
HANDLE pRead1, pRead2;//读文件线程事件(信号灯)
HANDLE pWrite1, pWrite2;//写文件线程事件(信号灯)
HANDLE ReadThreadID, WriteThreadID, EndThreadID;//保存各县城句柄
DWORD WINAPI ReadFileData( LPVOID lpParam )
{
FILE* fp;
CString FileName = (BSTR)lpParam;
::SysFreeString((BSTR)lpParam);
//初始化用户数据
Buf1Size = Buf2Size = BUFSIZ;
if( FileName.IsEmpty() || !(fp = fopen(FileName, "rb")) ){
Buf2Size = Buf1Size = 0;
return 0;
}
for(; Buf2Size == BUFSIZ; ){
WaitForSingleObject(pWrite1, INFINITE);
ResetEvent(pWrite1);
Buf1Size = fread(Buf1, 1, BUFSIZ, fp);
SetEvent(pRead1);
if(Buf1Size < BUFSIZ)
break;
WaitForSingleObject(pWrite2, INFINITE);
ResetEvent(pWrite2);
Buf2Size = fread(Buf2, 1, BUFSIZ, fp);
SetEvent(pRead2);
}
fclose(fp);
return 1;
}
DWORD WINAPI WriteFileData( LPVOID lpParam )
{
FILE* fp;
CString FileName = (BSTR)lpParam;
::SysFreeString((BSTR)lpParam);
SetEvent(pWrite1);
SetEvent(pWrite2);
if( FileName.IsEmpty() || !(fp = fopen(FileName, "wb")) )
return 0;
for(; Buf1Size + Buf2Size > 0; ){
WaitForSingleObject(pRead1, INFINITE);//等待获得对数据块1的操作
ResetEvent(pRead1);
fwrite(Buf1, Buf1Size, 1, fp);
SetEvent(pWrite1);
if(Buf1Size < BUFSIZ)
break;
WaitForSingleObject(pRead2, INFINITE);//等待获得对数据块2的操作
ResetEvent(pRead2);
fwrite(Buf2, Buf2Size, 1, fp);
SetEvent(pWrite2);
if(Buf2Size < BUFSIZ)
break;
}
fclose(fp);
return 1;
}
DWORD WINAPI EndFileCopy( LPVOID lpParam )
{
DWORD ReadThreadExitCord;
DWORD WriteThreadExitCord;
//WaitForSingleObject(ReadThreadID, INFINITE);//等待读进程结束
WaitForSingleObject(WriteThreadID, INFINITE);//等待写进程结束
TerminateThread(ReadThreadID, -1);
GetExitCodeThread(ReadThreadID, &ReadThreadExitCord);
GetExitCodeThread(WriteThreadID, &WriteThreadExitCord);
if(ReadThreadExitCord >0 && WriteThreadExitCord >0)
AfxMessageBox("文件复制完成");
else if( !ReadThreadExitCord )
AfxMessageBox("错误:源文件无法打开");
else
AfxMessageBox("错误:无法创建(更新)目标文件");
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CFileCopyDlg dialog
CFileCopyDlg::CFileCopyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFileCopyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFileCopyDlg)
// 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 CFileCopyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFileCopyDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFileCopyDlg, CDialog)
//{{AFX_MSG_MAP(CFileCopyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_SRC, OnButtonSrc)
ON_BN_CLICKED(IDC_BUTTON_DST, OnButtonDst)
ON_BN_CLICKED(ID_COPY, OnCopy)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileCopyDlg message handlers
BOOL CFileCopyDlg::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
//创建线程同步事件
pRead1 = CreateEvent(NULL, true, false, "FileReadReady1");
pRead2 = CreateEvent(NULL, true, false, "FileReadReady2");
pWrite1 = CreateEvent(NULL, true, false, "FileWriteReady1");
pWrite2 = CreateEvent(NULL, true, false, "FileWriteReady2");
return TRUE; // return TRUE unless you set the focus to a control
}
BOOL CFileCopyDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
return CDialog::DestroyWindow();
}
void CFileCopyDlg::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 CFileCopyDlg::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 CFileCopyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CFileCopyDlg::OnButtonSrc()
{
// TODO: Add your control notification handler code here
CFileDialog OpenFile(true);
OpenFile.DoModal();
CWnd* pEdit = GetDlgItem(IDC_EDIT_SRC);
pEdit->SetWindowText(OpenFile.GetPathName());
}
void CFileCopyDlg::OnButtonDst()
{
// TODO: Add your control notification handler code here
CFileDialog OpenFile(true);
OpenFile.DoModal();
CWnd* pEdit = GetDlgItem(IDC_EDIT_DST);
pEdit->SetWindowText(OpenFile.GetPathName());
}
void CFileCopyDlg::OnCopy()
{
// TODO: Add your control notification handler code here
CString ReadFile;
CString WriteFile;
GetDlgItem(IDC_EDIT_SRC)->GetWindowText(ReadFile);
GetDlgItem(IDC_EDIT_DST)->GetWindowText(WriteFile);
DWORD dwThreadId;
if( WaitForSingleObject(EndThreadID, 0) == WAIT_TIMEOUT ){
//多线程正在运行
AfxMessageBox("文件复制线程正在运行,请稍等");
return;
}
//创建文件复
FileCopy1.rar_多文件复制_多线程复制
版权申诉
163 浏览量
2022-09-22
18:34:08
上传
评论
收藏 11KB RAR 举报
小波思基
- 粉丝: 70
- 资源: 1万+
最新资源
- 由 Top10 开发和使用的惯用 Scala Redis 客户端 这是一项正在进行的工作,虽然在生产中使用,但应将其视为在生产中
- SwiftUI - SceanAppDelegate
- SwiftUI - AppDelegate
- Cetus是由C语言开发的关系型数据库MySQL的中间件,主要提供了一个全面的数据库访问代理功能
- 5152单片机proteus仿真和源码用定时器T0的中断实现渴望主题曲的播放
- 数据库垂直分区,业务逻辑层 配合数据库中间件实现水平分区和垂直分区
- 5152单片机proteus仿真和源码用定时器T0的中断控制1位LED闪烁
- 这是用于在 Akka 集群中复制数据的库的早期预览 它是一个复制的内存数据存储,支持低延迟和高可用性 要求
- 基于ketama算法和eredis项目的redis erlang驱动,主要以一致性hash的方式存储数据,做到key的分布式存储
- 2024五一杯B题要点和难点建模解析
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈