473,503 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread GUI problem

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();
}
}
}
Nov 16 '05 #1
4 1159
denton <de*******@yahoo.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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]
Nov 16 '05 #3
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.

"Richard Blewett [DevelopMentor]" wrote:
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]

Nov 16 '05 #4
denton <de*******@yahoo.com> wrote:
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/t...winforms.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2315
by: Tony Proctor | last post by:
Hi everyone We're experiencing some serious anomalies with the scheduling of ASP threads. I'd be interested to hear if anyone knows what algorithm is used (e.g. simple round-robin, or something...
7
2684
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
6
23707
by: Tomaz Koritnik | last post by:
I have a class that runs one of it's method in another thread. I use Thread object to do this and inside ThreadMethod I have an infinite loop: While (true) { // do something Thread.Sleep(100);...
7
5881
by: Sin Jeong-hun | last post by:
Hi. I'm writing a Client/Multi-threaded Server program on Windows Vista. It worked fine on Windows Vista, but when the server ran on Windows XP, I/O operation has been aborted because of either...
34
2749
by: Creativ | last post by:
Why does Thread class not support IDisposable? It's creating quite some problem. Namely, it can exhaust the resource and you have not control over it.
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.