473,503 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Form.Invoke() got me down

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
1 4202
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2869
by: Glen | last post by:
I have a Struts action form which contains a bean. I am trying to display a bean retrieved from the database in this form using the nested tag. Can anyone help me? I continue to get an error...
11
12778
by: Ahmet AKGUN | last post by:
Hi; is it possible to open one form in .net platform that we have its name in string ? I have string sFormName = "frmCustomer"; and I must automatically open Customer form. or is it...
5
8085
by: Stephen Lamb | last post by:
I have a background worker thread which I start from a form's HandleCreated event that makes calls back to the form using Invoke. During shutdown the form is disposed and the background worker...
3
7367
by: Brian Keating EI9FXB | last post by:
Hello again, I've already placed a few posts on this topic. This time i've a simple application that exhibits my problem, I've placed sample solution 8k on my website should anyone be interested...
8
16317
by: Brian Keating EI9FXB | last post by:
Would I be correct in saying that the only way to get a user message into a Windows form would be to use P/Invoke with Message? Of is there some part of the .NET API that I am totally un aware...
4
1671
by: Robin Tucker | last post by:
Some thread confusion to lighten up your day: I have a worker thread and a main form. When the user clicks the Close icon in the top right of the form, my program tells the thread to "stop". ...
2
2837
by: joey.powell | last post by:
I have a windows forms app with a statusbar. In the form's code, I use a delegates and spawn a worker thread to get processing off of the GUI thread. Then in the worker thread I access a function...
22
3035
by: Zytan | last post by:
I have public methods in a form. The main form calls them, to update that form's display. This form is like a real-time view of data that is changing. But, the form may not exist (it is...
3
4139
by: stumorgan | last post by:
Basically what I have is a form with a graph on it which graphs data that I'm reading from a USB device at 100 Hz (every 10ms). I have a thread reading and parsing the data from the USB, but when...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.