473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Invoke method causing dead lock ....

It seems that my program is dead lock after running this sentence (bold)

private delegate void updateBuildingDetailCallBack(String key, String Value);

private void updateBuildingDetail(String key, String Value)
{
if (tabBuildings.Controls["lblBuild_Resource_" + key].InvokeRequired)
{
// This is not a UI thread, we cannot update the UI directly
updateBuildingDetailCallBack callBack = delegate(String _key, String _Value)
{
tabBuildings.Controls["lblBuild_Resource_" + _key].Text = _Value;
};
tabBuildings.Controls["lblBuild_Resource_" + key].Invoke(callBack, new object[] { key, Value });
}
else
{
tabBuildings.Controls["lblBuild_Resource_" + key].Text = Value;
}
}

When I try to debug the program, if the program first time calling the function "updateBuildingDetail", the objects of tabBuildings.Controls["lblBuild_Resource_" + key] is still need to be invoked while normally for the first time it doesn't

May I know why cause the program hang after the bolded statement?
Thanks
Jul 19 '07 #1
4 4004
May I know why cause the program hang after the bolded statement?

Well, what causes updateBuildingDetail to get called? which thread?
How do you start this work? A BackgroundWorker? A callback? What?
If the UI thread is still waiting on a background thread to return
(which it shouldn't necessarily), then yes: this will indeed deadlock.
If the UI is available, it should process the item and return.

If you just care that it happens (not that it happens *right now*),
then just change to BeginInvoke. Also, I wouldn't start navigating the
Controls collection, as you aren't on the UI thread... I'd change this
line to simply use the local (this) InvokeRequired/Invoke/BeginInvoke;
i.e.:

if(InvokeRequired) {
BeginInvoke((MethodInvoker) delegate {
tabBuildings.Controls["lblBuild_Resource_" + key].Text = value;
});
} else {
tabBuildings.Controls["lblBuild_Resource_" + key].Text = value;
}

Note also that Invoke/BeginInvoke are safe to call from the UI thread;
if this is generally going to be called on a non-UI thread (due to the
use-case) I might even remove the "if" etc, and simply
Invoke/BeginInvoke it.

Marc
Jul 19 '07 #2
updateBuildingDetail is called by a child (non-UI) thread, which this child
thread is created by the main thread.

I found that in the child thread, the invokeRequired of
tabBuildings.Controls["lblBuild_Resource_" + key] is already true..
So I think the program do not know where is the original UI Thread....

As that UI Control is controlled by the code, such as lblBuild_Resource_01,
lblBuild_Resource_02, etc...
So I use Controls Method to change all of those label.

Thanks
Jul 19 '07 #3
and i think the main thread is continuously run

Meaning what? What does the code do after myThread.Start()? It should
return to the UI, letting the form paint itself, and process its
message-queue (which is necessary for Invoke/BeginInvoke). It it sits
in a tight-loop, then this approach is doomed (short of hacky hacks).

Re the arguments exception, do you get this using the MethodInvoker
(captured variables) approach? This is often a far easier way of
creating such a delegate... and *in this case* is entirely safe since
the strings are immutable, and the variables aren't updated inside the
method (there are some gotchas to look for).

Marc
Jul 19 '07 #4
Thank you very much, I solve the problem by creating another class and move
all the function to that class...
So weird~~
"Marc Gravell" <ma**********@gmail.comwrote in message
news:Ou**************@TK2MSFTNGP04.phx.gbl...
>and i think the main thread is continuously run

Meaning what? What does the code do after myThread.Start()? It should
return to the UI, letting the form paint itself, and process its
message-queue (which is necessary for Invoke/BeginInvoke). It it sits in a
tight-loop, then this approach is doomed (short of hacky hacks).

Re the arguments exception, do you get this using the MethodInvoker
(captured variables) approach? This is often a far easier way of creating
such a delegate... and *in this case* is entirely safe since the strings
are immutable, and the variables aren't updated inside the method (there
are some gotchas to look for).

Marc

Jul 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Charles Law | last post by:
I have a user control created on the main thread. Let's say, for arguments sake, that it has a single property that maintains a private variable. If I want to set that property from a worker...
3
by: David Logan | last post by:
I have an application using sockets, and it uses the asynchronous method for receiving (and others, but receiving is the basic problem.) In short, I want: class someClass: Form {...
5
by: Duncan Mole | last post by:
Hi all, Does anyone know of a way to achieve multi-theaded platform invokes? I have been taking quite a performance hit using the syncronised attribute: . Cheers ---
5
by: User N | last post by:
I have a log class with static methods that grab and release a mutex as required. The log class is designed to be called from both GUI and thread pool threads, and dump output to a logfile and a...
2
by: Chris Langston | last post by:
I have a Web Server running IIS 5 or 6 on Windows 2K and Windows 2003 Server that is experiencing strange shutdown problems. We are using ASP.NET v1.1 and our application is written in VB.NET ...
17
by: euan_woo | last post by:
Hi, Sometimes my program stops and when I break it I see 2 threads both waiting at a lock statement trying to lock the same object. If I look up the call stack of these threads there aren't any...
4
by: Chris Dunaway | last post by:
I have a main form with a "lock" button. When the lock button is clicked, another form is shown using ShowDialog(this). The user must enter their PIN on this form to close it and resume the main...
2
by: archana | last post by:
Hi all, I am having application wherein i am using asynchronous programming and using callback i am updating one label control. My question is can i use lock statment to lock control. If yes...
12
by: better_cs_now | last post by:
Hello all, I suspect that a threading issue is leading to an object being destructed in one thread context while one of its member functions is still running in another thread context. As a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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
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...

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.