Q: Using the ThreadPool class
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.
public void Work()
{
Bitmap bmpFromLibrary = new Bitmap(m_path);
m_outGraph.DrawImage(bmpFromLibrary, m_xAxis, m_yAxis, m_outRegionWidth, m_outRegionHeight);
}
In another class I want to do the following thing:
Render o_Render = new Render(path, graphOutput, outputXaxis, outputYaxis, outputRegionWidth, outputRegionHeight);
ThreadPool.QueueUserWorkItem(new WaitCallback(o_Render.Work));
But it keeps giving me the following error:
Error 1 No overload for 'Work' matches delegate 'System.Threading.WaitCallback'
Any ideas how to make this work?
Thanks in advance,
kiril
[993 byte] By [
kirchu] at [2007-12-28]
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));
}
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?