473,549 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(buff er, 0, bufferCount);

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

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

protected override void Dispose(bool disposing)
{
Stop = true;
downloadThread. Join();
OpenForms.MainF orm.LockDownloa d(false);

if (disposing && (components != null))
{
components.Disp ose();
}
base.Dispose(di sposing);
}
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(upd Deleg, 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 3525
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.comwrot e in message
news:10******** *************** ****@news.chell o.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(upd Deleg, 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.comwrot e 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.comwrot e in message
news:10******** *************** ****@news.chell o.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(buff er, 0, bufferCount);
|
| // Update information on control
| this.Invoke(upd Deleg, store);
| }
|
| Stop variable is interlocked because i use it also in Dispose method:
|
| protected override void Dispose(bool disposing)
| {
| Stop = true;
|
|
| downloadThread. Join();
|
|
| OpenForms.MainF orm.LockDownloa d(false);
|
| if (disposing && (components != null))
| {
| components.Disp ose();
| }
| base.Dispose(di sposing);
| }
|
|
| 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(upd Deleg, 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
3651
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 , /webapps/sue) and i have added the folders bob, sue in my server.xml(in the context path). When i am trying to run jsp files from my browser, it works...
4
1117
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 different thread". I think this occurs because the grid wasn't created during the thread. How would I be able to set its datasource from within the...
1
1490
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 write a message to my GUI textbox, but when I access the API from another THREAD , API try to call this function (reference to output message function),...
4
6833
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; for a 200 object set I don't see the exception. I believe the salient point is the Out Of Memory bit - marked below by *****'s The fact that...
2
3149
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( O_Constants.DB_CONNECTION_URL, O_Constants.DB_CONNECTION_USERNAME, O_Constants.DB_CONNECTION_PASSWORD); st =...
2
5715
by: mathersj | last post by:
Hello I was wondering whether anybody knew what this error may be telling me: HTTP Status 500 - -------------------------------------------------------------------------------- type Exception report
1
4361
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 program is as follows import java.sql.*; import java.lang.*; import java.io.*; import java.util.*;
11
5367
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 response error shown after the code, with the description "Parameters xmldata, $filestream and $filedata are all missing. One of these must not be...
0
1321
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 quite confused with this one from the website:====================================== Assuming you've unpacked Tomcat as ~/local/tomcat, then the Nutch...
0
7518
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
7715
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7469
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
6040
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...
1
5368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5087
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
3498
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...
1
1057
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.