By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,877 Members | 1,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,877 IT Pros & Developers. It's quick & easy.

Form.Invoke() got me down

P: n/a
I need to update my UI from a Process or worker thread. I did some readinf
and basically ended up adapting an MS example to fot my needs. It all made
sense until I tried it :)

My process makes calls to a Singleton logger class which in turn makes calls
to a delegate to add items to a listbox in my WinForm.

Here is the code that I have in my Form class to log output
<code>
delegate void AddOutputItem(string msg);

public void LogOutput(string msg)
{
ListViewItem item = new ListViewItem(msg);
if (this.listView_Output.InvokeRequired)
{
try
{
AddOutputItem d = new AddOutputItem(LogOutput);
this.Invoke(d, new object[] { msg });
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
}
}
else
{
this.listView_Output.Items.Insert(0, item);
}
}
</code>
The above code seems to work correct until it gets to the Invoke() call.
That is, UI thread calls to LogOutput have InvokeRequired = false and the
calls coming from my Process have it set to true. Once it hits that
Invoke() it hangs. No exceptions, nothing.. jsut stalls out.

Anyone have any ideas? Without exceptions I'm not really sure what to do.

Thanks for reading,
Steve
Feb 24 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Here is a sample:

private delegate void UpdateItemHandler(string msg);

private void MethodCalledFromEitherBackgroundOrUIThread( string data
) {
// Check if Method called from the non-UI thread
if( lv.InvokeRequired ) {
lv.Invoke( new UpdateItemHandler( UpdateItem ), new object[]{
"hello world msg"} );
}
else {
UpdateItem("how are you msg");
}
}

private void UpdateItem(string message ) {
lv.Items.Add( message );
....
}

Try and see if something like this works. In your scenario, you set
AddOutputItem to be the same public method LogOutput. So, the
background thread enters the LogOutput method and then you call Invoke,
which causes a switch to the foreground thread to update the UI
control, meanwhile the Invoke should have returned and exited the
function. I don't know why your background thread is not exiting the
function. However, try something like the sample above, where the
delegate is different and see if that would solve your problem.

Sincerely,
Bobby

Steve wrote:
I need to update my UI from a Process or worker thread. I did some readinf
and basically ended up adapting an MS example to fot my needs. It all made
sense until I tried it :)

My process makes calls to a Singleton logger class which in turn makes calls
to a delegate to add items to a listbox in my WinForm.

Here is the code that I have in my Form class to log output
<code>
delegate void AddOutputItem(string msg);

public void LogOutput(string msg)
{
ListViewItem item = new ListViewItem(msg);
if (this.listView_Output.InvokeRequired)
{
try
{
AddOutputItem d = new AddOutputItem(LogOutput);
this.Invoke(d, new object[] { msg });
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
}
}
else
{
this.listView_Output.Items.Insert(0, item);
}
}
</code>
The above code seems to work correct until it gets to the Invoke() call.
That is, UI thread calls to LogOutput have InvokeRequired = false and the
calls coming from my Process have it set to true. Once it hits that
Invoke() it hangs. No exceptions, nothing.. jsut stalls out.

Anyone have any ideas? Without exceptions I'm not really sure what to do.

Thanks for reading,
Steve


Feb 24 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.