标签 多线程 下的文章

传递多个参数给_beginthread

/*******Test Return Value of Struct *******/
#include <stdio.h>
#include <process.h>

typedef struct ST{ 
	int num; 
	char* buf; 
}thestruct; 

void Addem(thestruct* );

int main()
{
	thestruct st;
	st.num = 6000;
	_beginthread((void(*)(void*))Addem,0,(void*)&st);
	getchar();    // freeze program so you can see output
}

void Addem(thestruct* s)
{
	printf("%d\n",(thestruct*)(s)->num);
	_endthread();
}

解决方法就是使用struct。

[via]

wininet源代码获取例子 update:使用多线程,修正点击按钮后卡住的问题

#include <windows.h>
#include <wininet.h>
#include <process.h>
#pragma comment(lib, "wininet")

#define ID_URLEDITBOX 1
#define ID_GETBUTTON 2
#define ID_SHOWEDITBOX 3
static HWND hwndUrlEdt,hwndGetBtn,hwndShowEdt;

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("BtnLook") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;

wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd = CreateWindow (szAppName, TEXT ("WinINet源代码获取"),
WS_OVERLAPPEDWINDOW,
GetSystemMetrics (SM_CXSCREEN) / 4, GetSystemMetrics (SM_CYSCREEN) / 4,
GetSystemMetrics (SM_CXSCREEN) / 2, GetSystemMetrics (SM_CYSCREEN) / 2,
NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
void Thread1 (PVOID pvoid)
{
HINTERNET hConnect,hRequest,hSession;
LPTSTR lpszHost;
char szBuffer[1024*1024/10];
TCHAR szStr[100];
DWORD dwSize=0;
DWORD dwDownloaded;
GetWindowText(hwndUrlEdt,szStr,sizeof(szStr)/sizeof(TCHAR));
hSession = InternetOpen(TEXT("testWinINet"), PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0);
hConnect = InternetOpenUrl(hSession,szStr,NULL,0,INTERNET_FLAG_DONT_CACHE,0);
ZeroMemory(szBuffer,sizeof(szBuffer));
while (InternetReadFile(hConnect,szBuffer,sizeof(szBuffer),&dwDownloaded))
{
if (0==dwDownloaded) break;
szBuffer[dwDownloaded]=0;
}
SetWindowText(hwndShowEdt,szBuffer);
ZeroMemory(szBuffer,sizeof(szBuffer));
_endthread () ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
hwndUrlEdt = CreateWindow (TEXT ("EDIT"), NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL ,
20,20,500,20, hwnd, (HMENU)ID_URLEDITBOX,
((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;

hwndGetBtn = CreateWindow (TEXT ("BUTTON"), TEXT("GO"),
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON ,
530,20,30,20, hwnd, (HMENU)ID_GETBUTTON,
((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;

hwndShowEdt = CreateWindow (TEXT ("EDIT"), TEXT(""),
WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE ,
20,60,600,250, hwnd, (HMENU)ID_SHOWEDITBOX,
((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;

return 0;
case WM_COMMAND:
if(LOWORD(wParam) == ID_GETBUTTON)
{
_beginthread (Thread1, 0, NULL) ;
}
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

多加入了#include ,project setting->code generation设置为multithreaded,HWND的声明放到了全局范围,便于Thread1()的访问。_beginthread (Thread1, 0, NULL) ;进入线程,_endthread ()结束线程 ;

visual studio的debug窗口在点击一次“GO”按钮后,会弹出消息:
The thread 0xDA4 has exited with code 0 (0x0).
The thread 0xE1C has exited with code 0 (0x0).
The thread 0xCC0 has exited with code 0 (0x0).
The thread 0xB90 has exited with code 0 (0x0).
The thread 0xBF8 has exited with code 0 (0x0).
The thread 0xF14 has exited with code 0 (0x0).
The thread 0xA58 has exited with code 0 (0x0).
The thread 0xDA8 has exited with code 0 (0x0).
当一个线程还没有结束的时候,可以多次重复点击“GO”按钮,新建新的线程。

C语言Win32多线程简单例子

#include <windows.h>
#include <stdio.h>
#include <process.h>
// Secound Thread function
void ThreadProc(void *param);

// First thread
int main()
{

int n;
int i;
int val = 0;
HANDLE handle;

printf(" Thread Demo
");

printf("Enter the number of threads : ");
scanf("%d",&n);

for(i=0;i<n ;i++)
{
val = i+1;
handle = (HANDLE) _beginthread( ThreadProc,0,&val); // create thread
WaitForSingleObject(handle,INFINITE);

}

return 0;
}

void ThreadProc(void *param)
{

int h=*((int*)param);
printf("%d Thread is Running!
",h);
Sleep(1000);
_endthread();

}

功能是执行用户键入指定数量的线程,每个线程干同样的事情:输出字符串:某Thread is Running!,然后Sleep 1秒钟,结束进程,WaitForSingleObject等待指定对象进入有信号的状态,或者等待指定时间。 另外发现一个监视线程活动的好工具,sysinternal的

C语言Win32多线程简单例子

参考:http://www.codeproject.com/KB/threads/crtmultithreading.aspx