Re: Yay!! 27 years!



On Fri, 09 Sep 2005 18:06:33 +0200, JP Morris <jpm@xxxxxxxxx> wrote:

pibbur wrote:
On Fri, 09 Sep 2005 16:04:37 +0200, Lord Vashnu Dragon <lordv@xxxxxxxxxxxx> wrote:


Or, a shorter version:

while (1) {}

 I prefer this one:
 for(;1;) {}

This should work:

for(;;);

You're quite right.

This is BTW the VS.net 2003 C++ version - I've removed most (not all) bells and whistles. The loop is activated by clicking in the window.


// EndlessLoop.cpp.cpp : Defines the entry point for the application. // // EndlessLoop.cpp.cpp : Defines the entry point for the application. //

#include "stdafx.h"
#include "EndlessLoop.cpp.h"
#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_ENDLESSLOOPCPP, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_ENDLESSLOOPCPP);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;


	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_ENDLESSLOOPCPP);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCTSTR)IDC_ENDLESSLOOPCPP;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;


   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;


	switch (message)
	{

	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;

	case WM_LBUTTONDOWN:

		for(;;) ;
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;


	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		break;
	}
	return FALSE;
}

The C# version is of course more compact:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	///
	public class Form1 : System.Windows.Forms.Form
	{
		static Form1 Form;

		private System.Windows.Forms.Label label1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			//
			// label1
			//
			this.label1.Location = new System.Drawing.Point(40, 40);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(256, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "Click to start endless loop";
			//
			// Form1
			//
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(528, 454);
			this.Controls.Add(this.label1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Click += new System.EventHandler(this.Form1_Click);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Form=new Form1();
			Application.Run(Form);

		}

		private void Form1_Click(object sender, System.EventArgs e)
		{
			Form.label1.Text="Endless loop started";
			Form.Refresh();
			for(;;)
			{
				;
			}
		
		}
	}
}
--
MessageBox.Show("Pibbur wrote this");

Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
.



Relevant Pages

  • Child window WM_PAINT messages not delivered. Why?
    ... I have an app where I have a modeless dialog embedded within the client area ... All is well except when another window is moved around ... static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, ... LPARAM lParam); ...
    (microsoft.public.win32.programmer.ui)
  • Re: CreateProcess() its extermely slow
    ... I notice that if my application has no Window, ... int APIENTRY _tWinMain(HINSTANCE hInstance, ... LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, ... LPARAM lParam) ...
    (microsoft.public.vc.mfc)
  • Re: WM_KEYDOWN does not work
    ... HWND g_hwndClientDlg; ... // PURPOSE: Registers the window class. ... LRESULT CALLBACK ClientDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, ... LPARAM lParam) ...
    (microsoft.public.pocketpc.developer)
  • Re: WM_KEYDOWN does not work
    ... HWND g_hwndClientDlg; ... // PURPOSE: Registers the window class. ... LRESULT CALLBACK ClientDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, ... LPARAM lParam) ...
    (microsoft.public.pocketpc.developer)
  • Re: Need simplest sample Windows CE application ever coded
    ... Drawing a Window in dialog mode with a small red recangle. ... int WINAPI WinMain(HINSTANCE hInstance, ... // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) ... LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, ...
    (microsoft.public.windowsce.embedded)