Threading - newbie question

M

MAY

hi,
I have problem with simple threading. In a form and two button, if i click
one button the another button will flash. Here is the code i wrote, but it
make the application suspend... what wrong with it??? Thx...


bool ok;

public void test()
{
for(int i=0; i<1000; i++)
{
if(ok)
{
this.button1.Visible=true;
Console.WriteLine("show");
ok=false;
}
else
{
this.button1.Visible=false;
Console.WriteLine("hide");
ok=true;
}
Thread.Sleep(100);
}

}


private void button2_Click(object sender, System.EventArgs e)
{
Thread n=new Thread(new ThreadStart(test));
n.Name="test1";
n.IsBackground=true;
n.Start();
Thread.Sleep(30000); }
 
J

Jeff Foster

I'd say that the reason the application suspends is the sleep for 30 seconds
you're doing at the end of the click event on button 2.
 
M

Morten Wennevik

Hi MAY,

You are calling Thread.Sleep(30000) which will put the currently executing thread to sleep for 30+ seconds. In your case this is the main program thread. The other thread that you create will call Thread.Sleep(100) and only sleep for 0.1+ seconds, and then alternating the visible state of button1 every 0.1 second, which it does from you start, but you won't see it until your main GUI thread wakes up after about 30 seconds.

Btw, it is usually a bad idea to let any other thread than the main GUI thread access any GUI controls directly.

Happy coding!
Morten Wennevik [C# MVP]
 
M

MAY

Thx for reply!

Are there have alternative to do it?


MAY



Morten Wennevik said:
Hi MAY,

You are calling Thread.Sleep(30000) which will put the currently executing
thread to sleep for 30+ seconds. In your case this is the main program
thread. The other thread that you create will call Thread.Sleep(100) and
only sleep for 0.1+ seconds, and then alternating the visible state of
button1 every 0.1 second, which it does from you start, but you won't see it
until your main GUI thread wakes up after about 30 seconds.
Btw, it is usually a bad idea to let any other thread than the main GUI
thread access any GUI controls directly.
Happy coding!
Morten Wennevik [C# MVP]
 
M

Morten Wennevik

You could use properties and lock them to prevent more than one thread accessing the objects at any one time.

The problem with several threads using the same controls simultanously is that it can lead to corrupted data and unforseen trouble. For instance one thread is reading some data and another thread is changing the data. You may end up with partly read or written data. Locking ensures that corruption won't happen, but it also introduces the danger of deadlocking.


Happy coding!
Morten Wennevik [C# MVP]
 
M

MAY

Thanks for valuable reply.

I got System.InvalidOperationException is that the problem u descripted ?
How to perform locking to solve this suituation?

Thx!

MAY




Morten Wennevik said:
You could use properties and lock them to prevent more than one thread
accessing the objects at any one time.
The problem with several threads using the same controls simultanously is
that it can lead to corrupted data and unforseen trouble. For instance one
thread is reading some data and another thread is changing the data. You
may end up with partly read or written data. Locking ensures that
corruption won't happen, but it also introduces the danger of deadlocking.
Happy coding!
Morten Wennevik [C# MVP]
 
M

Morten Wennevik

Thanks for valuable reply.

I got System.InvalidOperationException is that the problem u descripted ?
How to perform locking to solve this suituation?

Thx!

MAY

I'm not too familiar with InvalidOperationException, so I can't say. If you get the same exception every time you run the program, it may be something else. If you get it randomly, chances are it's a threading problem.

Threads and locking are vast subjects that I know little about, and only beginning to grasp myself, so I won't go into specific solutions as they are probably not that good.


Happy coding!
Morten Wennevik [C# MVP]
 
M

MAY

Thats ok.
Your reply help me a lot. Thanks again ;)

MAY


Morten Wennevik said:
I'm not too familiar with InvalidOperationException, so I can't say. If
you get the same exception every time you run the program, it may be
something else. If you get it randomly, chances are it's a threading
problem.
Threads and locking are vast subjects that I know little about, and only
beginning to grasp myself, so I won't go into specific solutions as they are
probably not that good.
Happy coding!
Morten Wennevik [C# MVP]
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi MAY,

1. As long as use Windows forms never touch controls from a thread that has
not created them. In most cases this won't work.
2. When alter control's visibility what the framework do is to send the
control WM_WINDOWPOSCHANGED message. In your case the message has to cross
thread boundaries. When sending messages across threads windwos uses the
destination thread's message queue (yes even sent messages are pumped by the
message loop). If there is not message pump (loop) no sent messages are
received. That's why your button starts flickering with ~30 seconds delay
(while the main thread sleeps the message pump is not working).
 
T

TT \(Tom Tempelaere\)

MAY said:
hi,
I have problem with simple threading. In a form and two button, if i click
one button the another button will flash. Here is the code i wrote, but it
make the application suspend... what wrong with it??? Thx...

bool ok;
public void test()
{
for(int i=0; i<1000; i++) {
if(ok) {
this.button1.Visible=true;
Console.WriteLine("show");
ok=false;
}
else {
this.button1.Visible=false;
Console.WriteLine("hide");
ok=true;
}
Thread.Sleep(100);
}
}

private void button2_Click(object sender, System.EventArgs e)
{
Thread n=new Thread(new ThreadStart(test));
n.Name="test1";
n.IsBackground=true;
n.Start();
Thread.Sleep(30000); }

Hi,

Why not use one of the Timer classes if you need a timer?

HTH,
 

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