473,387 Members | 3,810 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,387 software developers and data experts.

error when invoke in thread

Hi. I have a form. This form calls thread.
Inside this thread i have while loop:

while ((bufferCount = FileStream.Read(buffer, 0, 64000)) 0
&& !Stop)
{
store += bufferCount;
file.Write(buffer, 0, bufferCount);

// Update information on control
this.Invoke(updDeleg, store);
}

Stop variable is interlocked because i use it also in Dispose method:

protected override void Dispose(bool disposing)
{
Stop = true;
downloadThread.Join();
OpenForms.MainForm.LockDownload(false);

if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
The problem is :
I dispose close form and this form is SOMETIMES ( mostly ) lagged. It's
never lagged when i delete this line from while loop:

// Update information on control
this.Invoke(updDeleg, store);
Here this simple invoked method:
private void update(long store)
{
// Update Progressbar here

}
Cant understand why its not working when using Invoke.
For any help i would be grateful
PK
Sep 21 '06 #1
5 3518
I have also observed that when i right click form on the taskbar, form is
closed immidiately and everything return to it's normal state.
Sep 21 '06 #2
"PiotrKolodziej" <pi*************@gmail.comwrote in message
news:10***************************@news.chello.pl. ..
The problem is :
I dispose close form and this form is SOMETIMES ( mostly ) lagged. It's
never lagged when i delete this line from while loop:

// Update information on control
this.Invoke(updDeleg, store);
What do you mean by "lagged"?

The code you've posted has the potential for deadlock, which would stop
execution altogether. The secondary thread calls Invoke and the primary
thread calls Join on the secondary thread.

If the secondary thread calls Invoke at any time just before, or any time
after, the primary thread calls Join, the primary thread will be sitting
there waiting for the secondary thread to complete while the secondary
thread will be sitting there waiting for the primary thread to process the
invoked delegate.

Deadlock.

The only way to fix it is to not write code in which both threads can at the
same time be waiting on the other.

Pete
Sep 21 '06 #3
Piotr,

Here what I believe the problem is - calling invoke on a control uses the
message loop to do the thread switching. That means the main UI thread must
run the message loop in order this to work. However when you call dispose
you join the download thread that cause the main UI thread to stop pumping
messages and if it happens just before the downloading thread to call the
Invoke method you get yourself in a deadlock situation. According to the
documentation Join keeps processing the massages that are sent, but in this
case the message that the Invoke method uses is posted, so the deadlock is
there.
--
HTH
Stoitcho Goutsev (100)

"PiotrKolodziej" <pi*************@gmail.comwrote in message
news:70**************************@news.chello.pl.. .
>I have also observed that when i right click form on the taskbar, form is
closed immidiately and everything return to it's normal state.


Sep 21 '06 #4

"PiotrKolodziej" <pi*************@gmail.comwrote in message
news:10***************************@news.chello.pl. ..
| Hi. I have a form. This form calls thread.
| Inside this thread i have while loop:
|
| while ((bufferCount = FileStream.Read(buffer, 0, 64000)) >
0
| && !Stop)
| {
| store += bufferCount;
| file.Write(buffer, 0, bufferCount);
|
| // Update information on control
| this.Invoke(updDeleg, store);
| }
|
| Stop variable is interlocked because i use it also in Dispose method:
|
| protected override void Dispose(bool disposing)
| {
| Stop = true;
|
|
| downloadThread.Join();
|
|
| OpenForms.MainForm.LockDownload(false);
|
| if (disposing && (components != null))
| {
| components.Dispose();
| }
| base.Dispose(disposing);
| }
|
|
| The problem is :
| I dispose close form and this form is SOMETIMES ( mostly ) lagged. It's
| never lagged when i delete this line from while loop:
|
| // Update information on control
| this.Invoke(updDeleg, store);
|
|
| Here this simple invoked method:
| private void update(long store)
| {
| // Update Progressbar here
|
| }
|
|
| Cant understand why its not working when using Invoke.
| For any help i would be grateful
| PK
|
|
Invoke post's a message and a delegate to the UI thread and waits for the
underlying delegate's function to return. Whenever you call Join in the UI
thread, you block the UI thread and as such it's message pump (well not
exactly, because Join performs some limitted pumping). That means that you
prevent the background thread to make progress, and as such you keep the UI
thread from making progress too, result: deadlock.

There are several way's to solve this issue:
- use BeginInvoke instead of Invoke,
- use some synchronization primitive, like an event, in order to coordinate
distinct thread's activities.
- use the Join overload with a timeout value (say 20 msec) in an loop in
order to pump some more messages, like ....
while(t.Join(20) == false)
Willy.

Willy.
Sep 21 '06 #5
Invoke post's a message and a delegate to the UI thread and waits for the
underlying delegate's function to return. Whenever you call Join in the UI
thread, you block the UI thread and as such it's message pump (well not
exactly, because Join performs some limitted pumping). That means that you
prevent the background thread to make progress, and as such you keep the
UI
thread from making progress too, result: deadlock.

There are several way's to solve this issue:
- use BeginInvoke instead of Invoke,
- use some synchronization primitive, like an event, in order to
coordinate
distinct thread's activities.
- use the Join overload with a timeout value (say 20 msec) in an loop in
order to pump some more messages, like ....
while(t.Join(20) == false)
Willy.

Thank you. It was really helpful.
PK
Sep 22 '06 #6

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

Similar topics

0
by: Jagdeesh | last post by:
Hai Colleagues, I am using Tomcat 4.1.24 and JDK 1.4.0_03 in my winXP machine. I've transferred a set of folders(containing jsp files) into tomcat's webapps directory(to /webapps/bob ,...
4
by: VM | last post by:
In the thread, I create a tableX and fill it with data. When I set the grid's source to this tableX, I get a compiler error "Controls created on one thread cannot be parented to a control on a...
1
by: [Yosi] | last post by:
I have an API, with a call back error definition. API receive reference to C# output message function (write to text box using Invoke since I use thread). Every thing is working fine API can...
4
by: Rachel McConnell | last post by:
Hello, I have a Java web application using Hibernate to connect to a PostgreSQL backend. I am seeing the below stack trace during processing of a set of data consisting of around 1000 objects;...
2
by: Suha Onay | last post by:
Hi, I have a problem with PostgreSQL 7.4. (With the old one 7.3 no probllem.) I create a connection and a statement : Class.forName("org.postgresql.Driver"); conn = DriverManager.getConnection(...
2
by: mathersj | last post by:
Hello I was wondering whether anybody knew what this error may be telling me: HTTP Status 500 - -------------------------------------------------------------------------------- type...
1
by: kommaraju | last post by:
iam a starter to db2 & jdbc.i have a servlet program which connects to ibm db2 using jdbc.when i run this using apache tomcat 4.1.34 , it is showing a error message of HTTP STATUS 500 my jdbc...
11
by: honguin | last post by:
Hi, With the following code, I have created a web request to a url which I am making a HTML POST with the html page request.htm, even though it makes a HTML POST, the StreamReader produces a XML...
0
by: archaaa | last post by:
Im trying to install nutch-0.7 on windows xp(sp 2) using cygwin and tomcat 4.0 under the guidance of http://lucene.apache.org/nutch/tutorial.html. All went well until the search was made. Im also...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...

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.