473,545 Members | 2,009 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(s tring msg);

public void LogOutput(strin g msg)
{
ListViewItem item = new ListViewItem(ms g);
if (this.listView_ Output.InvokeRe quired)
{
try
{
AddOutputItem d = new AddOutputItem(L ogOutput);
this.Invoke(d, new object[] { msg });
}
catch (Exception e)
{
System.Diagnost ics.Debug.Write Line(e.Message) ;
}
}
else
{
this.listView_O utput.Items.Ins ert(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 4208
Here is a sample:

private delegate void UpdateItemHandl er(string msg);

private void MethodCalledFro mEitherBackgrou ndOrUIThread( string data
) {
// Check if Method called from the non-UI thread
if( lv.InvokeRequir ed ) {
lv.Invoke( new UpdateItemHandl er( UpdateItem ), new object[]{
"hello world msg"} );
}
else {
UpdateItem("how are you msg");
}
}

private void UpdateItem(stri ng 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(s tring msg);

public void LogOutput(strin g msg)
{
ListViewItem item = new ListViewItem(ms g);
if (this.listView_ Output.InvokeRe quired)
{
try
{
AddOutputItem d = new AddOutputItem(L ogOutput);
this.Invoke(d, new object[] { msg });
}
catch (Exception e)
{
System.Diagnost ics.Debug.Write Line(e.Message) ;
}
}
else
{
this.listView_O utput.Items.Ins ert(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
2874
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 message see end for stack trace. Struts config <form-bean name="newBean" type="com.NewForm" /> ...... <action path="/new-action"...
11
12781
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 possible to get one referance of an item on form ?
5
8087
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 thread is aborted by the system. How is one to keep the background thread from calling form.Invoke after the form's window handle has been destroyed?...
3
7380
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 in having a look. http://briankeating.net/transfer/test.zip To recap the problem I expected (and found). I've a main GUI thead (main form), this...
8
16321
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 of? Thanks for any help Brian
4
1677
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". The thread will then complete its current operation and "Invoke" a method on the form telling it it has completed. The completed handler then calls...
2
2840
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 in a library file. The function needs to be able to update some of the forms status bar panels. I have had great success in doing the following... ...
22
3048
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 created / destroyed at user request). I can check form != null to prevent incorrect access. But, the form could disappear immediately after the check,...
3
4143
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 it comes time to draw that data on the graph the work is handled on the main UI thread through a callback delegate since the form owns the graph...
0
7475
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7409
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7771
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5982
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4958
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3465
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
720
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.