kirchu

Hello everybody. I have a little trouble using the ThreadPool class.
The logic is the following.

I have a class called Render and a method in it.


Re: Visual C# General Q: Using the ThreadPool class

Sean Hederman

Change Work to:

public void Work(object state)

{

// Code here

}






Re: Visual C# General Q: Using the ThreadPool class

kirchu

Thank you very much!
kiril





Re: Visual C# General Q: Using the ThreadPool class

fubak

Sean Hederman wrote:

Change Work to:

public void Work(object state)

{

// Code here

}



When I do this, I get this error in the 'Work' method.

"Parameter count mismatch."




Re: Visual C# General Q: Using the ThreadPool class

Sean Hederman

Double-check your code. ThreadPool.QueueUserWorkItem takes a WaitCallback which fits the signature I specified. If you still don't have any luck, post the code you're using so we can see if we can assist.






Re: Visual C# General Q: Using the ThreadPool class

fubak

Here's my code...

private delegate void showProgressPanelDelegate();

ThreadPool.QueueUserWorkItem(new WaitCallback(showProgressPanel));


public void showProgressPanel() {
try {
if (this.InvokeRequired) {
this.BeginInvoke(new showProgressPanelDelegate(showProgressPanel));
} else { // Safe to manipulate controls directly.
progressBarPanel.Visible = true;
progressBarPanel.BringToFront();
}
} catch (Exception ex) {
string tmp = ex.Message;
}
}






Re: Visual C# General Q: Using the ThreadPool class

Sean Hederman

WaitCallback must have an object parameter:

private delegate void showProgressPanelDelegate(object state);

ThreadPool.QueueUserWorkItem(new WaitCallback(showProgressPanel));


public void showProgressPanel(object state) {
try {
if (this.InvokeRequired) {
this.BeginInvoke(new showProgressPanelDelegate(showProgressPanel));
} else { // Safe to manipulate controls directly.
progressBarPanel.Visible = true;
progressBarPanel.BringToFront();
}
} catch (Exception ex) {
string tmp = ex.Message;
}
}




Re: Visual C# General Q: Using the ThreadPool class

fubak

Sean Hederman wrote:

WaitCallback must have an object parameter:

private delegate void showProgressPanelDelegate(object state);

ThreadPool.QueueUserWorkItem(new WaitCallback(showProgressPanel));


public void showProgressPanel(object state) {
try {
if (this.InvokeRequired) {
this.BeginInvoke(new showProgressPanelDelegate(showProgressPanel));
} else { // Safe to manipulate controls directly.
progressBarPanel.Visible = true;
progressBarPanel.BringToFront();
}
} catch (Exception ex) {
string tmp = ex.Message;
}
}


I tried that and it gave me a Parameter count mismatch on Application.Run




Re: Visual C# General Q: Using the ThreadPool class

Sean Hederman

Okay, I figured out the problem. You see WaitCallback expects an object parameter for your QueueUserWorkItem call. However, you're using BeginInvoke wrong. Rather use Invoke like I do below, and most importantly, note how I've passed in the null parameter using new object[] { null }. This is required so that the delegate dispatcher sees that there is a parameters (albeit an empty one), allowing it to marshal the call correctly.

You could use BeginInvoke, but then you must be sure to call EndInvoke on it. Also,since you're already in a worker thread, I don't see what Begin Invoke gains you.

private delegate void ShowProgressPanelDelegate(object state);

public void showProgressPanel(object state)

{

try

{

if (this.InvokeRequired)

{

this.Invoke(new ShowProgressPanelDelegate(showProgressPanel), new object[] { null });

}

else

{ // Safe to manipulate controls directly.

progressBarPanel.Visible = true;

progressBarPanel.BringToFront();

}

}

catch (Exception ex)

{

string tmp = ex.Message;

}

}

private void button1_Click(object sender, EventArgs e)

{

ThreadPool.QueueUserWorkItem(new WaitCallback(showProgressPanel));

}






Re: Visual C# General Q: Using the ThreadPool class

fubak

Awesome, that worked...now, how do I make that thread complete before moving on

This 'ProgressPanel' has a button on it that has an animated image on it. Right now, when the panel displays it shows a transparent box in the panel where the button should be. I'm assuming that I need to make the thread complete before moving on




Re: Visual C# General Q: Using the ThreadPool class

Sean Hederman

No, when the thread exits the showProgressPanel method it will have completed successfully, so there's nothing you need to do there. There must be some other reason why the panel is showing transparent.






Re: Visual C# General Q: Using the ThreadPool class

fubak

Nevermind, I called this.Update() on my thread to update the control. However, the image on the button is still not animated. Any ideas




Re: Visual C# General Q: Using the ThreadPool class

Sean Hederman

Not really, what kind of animated image is it






Re: Visual C# General Q: Using the ThreadPool class

fubak

it's the Mac 'Please Wait' icon. It's a bunch of lines in a circle and they change color one at a time and go in a circle. It's basically just a 'Thinking' icon.




Re: Visual C# General Q: Using the ThreadPool class

Sean Hederman

Sorry, I meant what image format is it GIF