CListCtrl中选择多项进行下载时的步骤:
void CMainFrame::OnFileDownload()
{
POSITION pos = m_List_Gps.GetFirstSelectedItemPositionwhile (pos)
{
++i;//全局变量,初始化为0
if(i!=1)
{
hEvent = CreateEvent (NULL, TRUE, FALSE,NULL);//hEvent定义为全局的 WaitForSingleObject(hEvent,INFINITE);
ResetEvent(hEvent);
}
iIndex = m_List_Gps.GetNextSelectedItem(pos);
FileDown.OnFileDownload(iIndex);//调用另个类的OnFileDownload() }
}LONG CMainFrame::OnCommunication(WPARAM ch, LPARAM port)
{
结束时:
SetEvent(hEvent);
}
OnFileDownload()最后是响应OnCommunication函数进行下载的,但他响应速度不怎么快,我要的就是判断每进行一次OnFileDownload()后,要等这次的OnCommunication响应完后才进行下一次的OnFileDownload(),但我按上面这样做不行啊,比如说两次时,在两次OnFileDownload()之间不会响应OnCommunication(),而是等两次OnFileDownload()响应完后才响应OnCommunication(),请问这是为什么??我用的不对吗???如果用Mutex的话也不行,不知为什么,大哥们帮我分析一下,谢谢!!!还要补充一点,每一次发生OnFileDownload(),OnCommunication是响应n(n>=1000)次的,我只是在其中判断响应的第n次(可能是999次)(结束了传输)时才SetEvent(hEvent);当然了,SetEvent(hEvent)还是响应一次的,是不是因为是这个原因啊??谢谢!
void CMainFrame::OnFileDownload()
{
POSITION pos = m_List_Gps.GetFirstSelectedItemPositionwhile (pos)
{
++i;//全局变量,初始化为0
if(i!=1)
{
hEvent = CreateEvent (NULL, TRUE, FALSE,NULL);//hEvent定义为全局的 WaitForSingleObject(hEvent,INFINITE);
ResetEvent(hEvent);
}
iIndex = m_List_Gps.GetNextSelectedItem(pos);
FileDown.OnFileDownload(iIndex);//调用另个类的OnFileDownload() }
}LONG CMainFrame::OnCommunication(WPARAM ch, LPARAM port)
{
结束时:
SetEvent(hEvent);
}
OnFileDownload()最后是响应OnCommunication函数进行下载的,但他响应速度不怎么快,我要的就是判断每进行一次OnFileDownload()后,要等这次的OnCommunication响应完后才进行下一次的OnFileDownload(),但我按上面这样做不行啊,比如说两次时,在两次OnFileDownload()之间不会响应OnCommunication(),而是等两次OnFileDownload()响应完后才响应OnCommunication(),请问这是为什么??我用的不对吗???如果用Mutex的话也不行,不知为什么,大哥们帮我分析一下,谢谢!!!还要补充一点,每一次发生OnFileDownload(),OnCommunication是响应n(n>=1000)次的,我只是在其中判断响应的第n次(可能是999次)(结束了传输)时才SetEvent(hEvent);当然了,SetEvent(hEvent)还是响应一次的,是不是因为是这个原因啊??谢谢!
ResetEvent(hEvent); 将CreateEvent移到其它地方。比如CMainFrame的构造函数。
这是什么意思,是不是另一个类里也有相同的处理函数OnFileDownload()?
WaitForSingleObject(hEvent,INFINITE);
在hEvent没有被SetEvent(hEvent);时应该是不会向下执行的,你可以试试设置断点看看到底是怎么回事?
{
POSITION pos = m_List_Gps.GetFirstSelectedItemPositionwhile (pos)
{
WaitForSingleObject(hEvent,INFINITE);
hEvent.ResetEvent();
iIndex = m_List_Gps.GetNextSelectedItem(pos);
FileDown.OnFileDownload(iIndex);//调用另个类的OnFileDownload()
}LONG CMainFrame::OnCommunication(WPARAM ch, LPARAM port)
{
结束时:
hEvent.SetEvent();
}hEvent为一个全局的 CEvent對象
i 看不出有什么用, 可以删掉
OnFileDownload()
和
OnCommunication
都是通过
CMainFrame的消息来处理的,
消息是已经经过排序的。
您的程序是可能导致死锁的。因为他们实际在同一个线程(主线程)中,
所以同步机制不能发挥作用您需要注意的是:
WaitForSingleObject
只能用于“线程”间的同步,
而不是实现“函数”间的同步,
也不能实现“消息处理”的同步!您需要加深对"同步机制"的理解。
OnCommunication ????如果您使用同步的方法,就不需要用消息了,
直接把OnCommunication做成一个线程,
通过Event来控制运行就可以了.
,这样不行吗???谢谢!!!
#include "StdAfx.h"
#include <iostream>
using namespace std;HANDLE hEvent = 0;
char ch = 'A';
bool bRun = false;ULONG _stdcall OnFileDownload(LPVOID);
void OnCommunication();int main()
{
hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
bRun = true;
HANDLE hThread = CreateThread(NULL,NULL,OnFileDownload,NULL,NULL,NULL);
for(int i=0;i<10;i++)
{
cin>>ch;
if(ch == '#')
{
break;
}
OnCommunication();
}
bRun = false; // Í£Ö¹ÔËÐÐ
SetEvent(hEvent); Sleep(2000);
CloseHandle(hThread);
hThread = 0;
CloseHandle(hEvent);
hEvent = 0;
cout << endl << "---- END ----" << endl;
return 0;
}ULONG _stdcall OnFileDownload(LPVOID)
{
while(bRun)
{
cout << endl << "---- Wait Begin ----" << endl;
WaitForSingleObject(hEvent,INFINITE);
ResetEvent(hEvent);
cout << endl << "---- Wait End ----" << endl;
for(int i = 0; bRun && (i < 10); i++)
{
cout << "i = " << i << " ch = " << ch << endl;
Sleep(300);
}
}
return 0;
}void OnCommunication()
{
static num =0;
num ++;
if(num%3 == 0)
{
cout << endl << "---- set event ----" << endl;
SetEvent(hEvent);
}
}/*
---- Wait Begin ----
1 // input 1,3,4
3
4
---- set event ----
---- Wait End ----
i = 0 ch = 4
i = 1 ch = 4
i = 2 ch = 4
i = 3 ch = 4
i = 4 ch = 4
i = 5 ch = 4
i = 6 ch = 4
i = 7 ch = 4
i = 8 ch = 4
i = 9 ch = 4---- Wait Begin ----
3 // input 3,4,5
4
5---- set event ----
---- Wait End ----
i = 0 ch = 5
6i = 1 ch = 5 // input 67i = 2 ch = 6 // input 7
i = 3 ch = 7
i = 4 ch = 7
i = 5 ch = 7
i = 6 ch = 7
i = 7 ch = 7
i = 8 ch = 7
i = 9 ch = 7
---- Wait Begin ----
# // // input #
---- Wait End ----
---- END ----
*/
,让OnCommunication响应,不过串口类是要SendMessage很多次的,所以OnCommunication也响应很多次,它是一个字符发一次的.
点击下载时调用CMainFrame里的OnFileDownload,OnFileDownload到最后时发出一个命令SendMessage,同过CserialPort类的中转,发送到GPS,GPS反馈信息回来,就这样.