Thread GUI problem

G

Guest

I want to read a line from the textbox just like a console window. The
called thread will not allow me to enter keys when I call Join. I have stuck
on this problem for 2 weeks:

private void button3_Click(object sender, EventArgs e)
{ ReadLine(); }

public string ReadLine()
{
Thread w1 = new Thread(new ThreadStart(ReadLineFunct));
w1.Start();
w1.Join();
return R_str; // field
}

void ReadLineFunct()
{
mre = new ManualResetEvent(false);
B_reading = true;
mre.WaitOne();
Debug.WriteLine("gotten in RL:" + R_str);
}

private void b_textbox_KeyDown(object sender, KeyEventArgs e)
{
if (B_reading)
{
if (e.KeyCode == Keys.Enter)
{
B_reading = false;
mre.Set();
}
else
{
string key = e.KeyCode.ToString();
if (key.Length == 1 || e.KeyCode == Keys.Space )
B_str += e.KeyCode.ToString();
}
}
}
 
J

Jon Skeet [C# MVP]

denton said:
I want to read a line from the textbox just like a console window. The
called thread will not allow me to enter keys when I call Join.

No, it wouldn't. Starting a thread and then immediately calling Join on
it is pretty much the equivalent of just calling the method directly.
You're not getting any benefit from it being multithreaded.

Read through http://www.pobox.com/~skeet/csharp/threads
 
R

Richard Blewett [DevelopMentor]

OK, the reason you cannot enter anything is that Join is a blocking call. In other words the thread that calls Join will block until the thread its waiting on exits.

And there is the problem, the thread that is blocking is the thread that is running the windows message pump and so no windows messages are being processed. This means nothing on your UI will change.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I want to read a line from the textbox just like a console window. The
called thread will not allow me to enter keys when I call Join. I have stuck
on this problem for 2 weeks:

private void button3_Click(object sender, EventArgs e)
{ ReadLine(); }

public string ReadLine()
{
Thread w1 = new Thread(new ThreadStart(ReadLineFunct));
w1.Start();
w1.Join();
return R_str; // field
}

void ReadLineFunct()
{
mre = new ManualResetEvent(false);
B_reading = true;
mre.WaitOne();
Debug.WriteLine("gotten in RL:" + R_str);
}

private void b_textbox_KeyDown(object sender, KeyEventArgs e)
{
if (B_reading)
{
if (e.KeyCode == Keys.Enter)
{
B_reading = false;
mre.Set();
}
else
{
string key = e.KeyCode.ToString();
if (key.Length == 1 || e.KeyCode == Keys.Space )
B_str += e.KeyCode.ToString();
}
}
}

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.809 / Virus Database: 551 - Release Date: 09/12/2004



[microsoft.public.dotnet.languages.csharp]
 
G

Guest

If I take out join the thread gets skipped and the string gets returned too
early. But then the called W1 thread works at accumulating the string,
although too late. Any suggestions of what I should use instead? Would you
use threading for this? Thanks.
 
J

Jon Skeet [C# MVP]

denton said:
If I take out join the thread gets skipped and the string gets returned too
early. But then the called W1 thread works at accumulating the string,
although too late. Any suggestions of what I should use instead? Would you
use threading for this? Thanks.

You need to make ReadLineFunct call back into the GUI with
Control.Invoke/BeginInvoke when it's finished, rather than making what
is essentially a blocking call in the UI thread.

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml
 

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