Threads in c#

Hello there.

im new to threading, so i tried looking on the net, i got so far but then i hit a problem. I could do very basic threads that did not really do anything. Howeer, what i have in mind i cannot find on the net, and the msdn library doesnt explain it very well.

Basically, when a user clicks "Search" i want a new thread to be created and the search happening on that thread. I tried to do it but i got an error message saying that the created thread cannot edit the Object View because it was not created on that thread. When i tried to solve it myself, it seems i need to use Delegates to allow a thread access to another threads resources (or something liek that). So basically, can anyone explain to me how i let a thread that i create, change things (like textboxes, listviews etc) on my interface.

I know i havent worded that well but its complicated to me, and i dont fully understand.

Regards,

Mark

[1524 byte] By [deviao9] at [2008-1-8]
# 1
Ok, let's suppose you have a method in your Form class that manipulates some controls on the Form, and that can be called from another thread. Assume that it is called "ManipulateControls()".

public void ManipulateControls()
{
// Do something with controls on the Form.
}

(1) Declare a delegate that you are going to use to transfer from the other thread to the main UI thread:

private delegate void ManipulateControlsDelegate();

(2) Change ManipulateControls() so that it checks InvokeRequired() and uses a delegate as needed:

public void ManipulateControls()
{
if (this.InvokeRequired)
{
this.Invoke(new ManipulateControlsDelegate(ManipulateControls));
}
else // Safe to manipulate controls directly.
{
// Do something with controls on the Form.
}
}

Now you can call ManiupulateControls() from another thread.

MatthewWatson at 2007-10-2 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

Thank you, this makes sense to me now.

deviao9 at 2007-10-2 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
I got this working, however, what I'm executing in the 'ManipulateControls' method doesn't execute until after the method that it's called from is finished executing. My code timeline is something like this...

1. Create and execute 'showPleaseWaitPanel' thread
2. Do some work on the form
3. Create and execute 'hidePleaseWaitPanel' thread

All 3 of these steps seem to be executing all at once. If I call Sleep for 5 seconds right after executing the 'showPleaseWaitPanel' thread, then nothing happens for 5 seconds and then everything executes. What am I doing wrong?

Thanks,
Brad

fubak at 2007-10-2 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
Try changing this.Invoke() to this.BeginInvoke().

The this.Invoke() will not return until the thing that it is invoking has returned. That, in turn, will not be run until the User Interface thread is not busy.

MatthewWatson at 2007-10-2 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
Ok, it's working...sorta. The 'showPleaseWaitPanel' has a button on it that has an animated image in it. That button isn't displaying...in fact, it's displaying a transparent hole in the panel the size of the button that should be there. Why is this?
fubak at 2007-10-2 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6
I added a AutoResetEvent.WaitOne after the Data Load method is done executing and that made the panel display the button...sometimes. Sometimes the button will show, sometimes i'll get the transparent hole, and sometimes the panel won't display at all. this is very odd Sad
fubak at 2007-10-2 > top of Msdn Tech,Windows Forms,Windows Forms General...