How to lock the Windows Form and wait for the user input?

F

Frankie Leung

Dear all,
I am writing a program (game) which will start processing when the user
had select an option from the ComboBox. Meanwhile, I want to make the
program to be able to listen to the user input whenever the application
is running. The execution of the KeyPress event from the Windows Form
must be processed first, even if other functions is processing.
However, the result of the running program does not seems to be behave
in this way. The program does not respond to the user input while it is
processing another function. Here is the code snippet for this program.
Extra code has been trimmed for simplicity and easy reading.

using System;

namespace AGame
{
partial class AGameMainFormDesigner : System.Windows.Forms.Form
{
// Constructor
public AGameMainFormDesigner (...) {
...
this.InitializeComponent();
}

void InitializeComponent()
{
this.cmbSelectLevel = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
// cmbSelectLevel
this.cmbSelectLevel.SelectedIndexChanged += new
System.EventHandler(this.cmbSelectLevel_SelectedIndexChanged);
// SpeechTypingGameMainFormDesigner
this.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(this.SpeechTypingGameMainFormDesignerKeyPress);
this.ResumeLayout(false);
}
private System.Windows.Forms.ComboBox cmbSelectLevel;

// Event functions
void cmbSelectLevel_SelectedIndexChanged(object sender,
System.EventArgs e)
{
this.PlayGame();
}

void SpeechTypingGameMainFormDesignerKeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
// Display the user input in the Windows Form
...
}

private void PlayGame() {
while (cond) {
// Do something
...
}
}
}
}

Any help will be greatly appreciated.

Best regards,
Frankie
 
K

Kevin Spencer

A Windows Form is a STA (Single-Threaded Apartment) application, meaning
that it runs on a single thread of execution. To do what you're talking
aoubt, you need multi-threading. This is not a simple task with Windows
Forms, but here are a few links to get you started:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms08162002.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms08162002.asp

--
HTH,

Kevin Spencer
Microsoft MVP
Bit Player
http://unclechutney.blogspot.com

A pea rants as candy be sieving.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top