Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is the following coding is correct?If yes how the same can be done in windows application?
static void Main(string[] args)
        {

            ThreadStart job = new ThreadStart(ThreadJob);
            Thread thread = new Thread(job);
            thread.Start();

            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Main thread: {0}", i);
                Thread.Sleep(1000);
            }
        }

        static void ThreadJob()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Other thread: {0}", i);
                Thread.Sleep(500);
            }
        }
Posted
Comments
Sergey Alexandrovich Kryukov 20-Sep-11 17:43pm    
Windows application? WPF, Forms, what? The answer depends on what UI library you use. Please tag it.
--SA

All right, this is correct code, not that it has a lot of sense, but could be a useful exercise.

One important improvement: the method passed to a thread constructor does not have to be static. This fact is poorly documented but it allows for much more powerful and safe approach to thread programming as the parameter "this" is automatically passed and it will give a thread a full access to the declaring class. This way, you can avoid parametrized thread start which is not so safe because of the need to do type cast.

The best way to use this is making a wrapper around your thread. Please see my past solution: How to pass ref parameter to the thread[^].

Now, Windows Applications. You should have tag the library you want to use, please see my comment. Just do it.
I'll explain the issue.

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
Yes, your code is correct but important point is what you want to acheive from it. below is the c++ code for the same.

void __cdecl ThreadJob(void*);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	// Windows inilization...
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	unsigned long ulThr1;
	
	switch(iMsg)
	{
		
		case WM_CREATE:

			ulThr1 = _beginthread(ThreadJob,0,(void*)hWnd);
		break;

		
		case WM_PAINT:
			int i=0; 
			char str[2];

			hDc=BeginPaint(hWnd,&ps);
			for(i=0; i<5; i++)
			{
				sprintf(str,"Main Thread -> No = %d",i);
				TextOut(hDc,5,5*(i*2),str,strlen(str));
			}
			EndPaint(hWnd,&ps);
		break;

		// Other cases...

	}
	return DefWindowProc(hWnd,iMsg,wParam,lParam);
}

void __cdecl ThreadJob(void* param)
{
	HDC hDc;
	int j;
	char str[2];

	hDc = GetDC((HWND)param);

	for(j=0; j<10; j++)
	{
		sprintf(str,"Other Thread -> No = %d",j);
		TextOut(hDc,50,5*(j*2),str,strlen(str));
	}
	ReleaseDC((HWND)param,hDc);
	
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900