473,406 Members | 2,549 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,406 software developers and data experts.

cross-thread operation

I am trying to make a client-server application to run, but I am
running into this problem:
in the server Form the call InitializeComponent(); creates a text-box.
Later the calls:
readThread = new Thread(new ThreadStart(RunServer));
readThread.Start();
attemt to update the textbox (in the function RunServer).
I get an exception saying that cross thread operation is invalid as
there is an attempt to access a texbox created in a nother thread.

what should I do?

Danny K

Oct 3 '06 #1
15 2353
..net it self it run on a thread so you are attampting to cross refer to the
application thread.

use form timer to do any UI updates.. System.Windows.Form.Timer will solve
this problem..

in ur case you may set a static variable which update by the RunServer
method where it will pick by the form timer and does the UI update time to
time.

Nirosh.

"dani kotlar" <da*********@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
>I am trying to make a client-server application to run, but I am
running into this problem:
in the server Form the call InitializeComponent(); creates a text-box.
Later the calls:
readThread = new Thread(new ThreadStart(RunServer));
readThread.Start();
attemt to update the textbox (in the function RunServer).
I get an exception saying that cross thread operation is invalid as
there is an attempt to access a texbox created in a nother thread.

what should I do?

Danny K

Oct 3 '06 #2
Simple; the spawned thread cannot talk to the TextBox directly; you need to
use .Invoke or .BeginInvoke to switch threads, and ask the UI thread to do
it on your behalf...

If you are just reading the value, another option is to read the value
*before* spawning the thread, and make the value available to the spawned
thread. In 2.0 this is easy with captured variables and anonymous delegates:

string currentText = textbox1.Text;
Thread readThread = new Thread(
(ThreadStart)delegate {
SomeFunctionWithParams(currentText);
});
readThread.Start();

Similarly, for updating:

string newValue = "abc";
textbox1.BeginInvoke((MethodInvoker) delegate {
textbox1.Text = newValue;
});

Note that BeginInvoke will run in parallel, so don't change newValue after
this, as the "textbox1.Text = newValue;" line might not have run yet. You
can use Invoke to run in series, but this can cause issues if the UI thread
is currently waiting for your thread to do something (deadlock). But it
probably shouldn't be anyway.

A final note - for updates you could also have a spooler type approach with
the two threads simply talking to (synchronised) objects / variables - and
the UI updating itself on a timer. This can allow you to combine multiple
logical updates in a single UI update, but requires a timer. Different hats
for different heads.

Marc
Oct 3 '06 #3
...you may set a static variable...

I wouldn't recommend "static" here... the OP didn't mention that the form
was a singleton, so using static could introduce bugs when multiple form
instances are open. As a general rule, keep things as tightly scoped as
possible; these variables relate to an instance of a form, so if using a
timer-based approach (see other post), then use form-level (instance) fields
/ properties.

Marc
Oct 3 '06 #4
yes you are correct, I forget it

"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>...you may set a static variable...

I wouldn't recommend "static" here... the OP didn't mention that the form
was a singleton, so using static could introduce bugs when multiple form
instances are open. As a general rule, keep things as tightly scoped as
possible; these variables relate to an instance of a form, so if using a
timer-based approach (see other post), then use form-level (instance)
fields / properties.

Marc

Oct 3 '06 #5
I did that and it works. Thank you.
However, pat fo the times I get the same exception as before, and in
other times I get an exception saying:
Invoke or BeginInvoke cannot be called on a control until the window
handle has been created.

What does this mean?
How do I fix it?
Thanks in advance.
Marc Gravell wrote:
Simple; the spawned thread cannot talk to the TextBox directly; you need to
use .Invoke or .BeginInvoke to switch threads, and ask the UI thread to do
it on your behalf...

If you are just reading the value, another option is to read the value
*before* spawning the thread, and make the value available to the spawned
thread. In 2.0 this is easy with captured variables and anonymous delegates:

string currentText = textbox1.Text;
Thread readThread = new Thread(
(ThreadStart)delegate {
SomeFunctionWithParams(currentText);
});
readThread.Start();

Similarly, for updating:

string newValue = "abc";
textbox1.BeginInvoke((MethodInvoker) delegate {
textbox1.Text = newValue;
});

Note that BeginInvoke will run in parallel, so don't change newValue after
this, as the "textbox1.Text = newValue;" line might not have run yet. You
can use Invoke to run in series, but this can cause issues if the UI thread
is currently waiting for your thread to do something (deadlock). But it
probably shouldn't be anyway.

A final note - for updates you could also have a spooler type approach with
the two threads simply talking to (synchronised) objects / variables - and
the UI updating itself on a timer. This can allow you to combine multiple
logical updates in a single UI update, but requires a timer. Different hats
for different heads.

Marc
Oct 3 '06 #6
Dani,

This is by design. Windows controls are connected to the thread that created
them and can be modified only by this thread. In order to modify the control
you execute the modifying code in the thread created the cotntrol. To do
that all controls provide Invoke and BeginInvoke methods. Use those methods
to marshal the excution to the UI thread and modify the control form there.
Google the newsgroups for Control.Invoke there are tons of posts with sample
code; there are also article on MSDN.
--
HTH
Stoitcho Goutsev (100)
"dani kotlar" <da*********@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
>I am trying to make a client-server application to run, but I am
running into this problem:
in the server Form the call InitializeComponent(); creates a text-box.
Later the calls:
readThread = new Thread(new ThreadStart(RunServer));
readThread.Start();
attemt to update the textbox (in the function RunServer).
I get an exception saying that cross thread operation is invalid as
there is an attempt to access a texbox created in a nother thread.

what should I do?

Danny K

Oct 3 '06 #7
Invoke and BeginInvoke work (IIRC) using the message pump, and this can only
happen once they have been created in the Win32 sense, and have Win32
handles.

This typically means you haven't shown your form yet, so Show() it;
alternatively, if you are subclassing Form (i.e. you have a MyForm.cs or
similar), then you might be able to get away with calling the protected
method CreateHandle(). Personally, I'd just Show() the form...

Does this help?

Marc
Oct 3 '06 #8
Marc,
I am not sure what you mean by Show(). The form was opened by creating
a new instance of the form:
Application.Run(new Server());
Can you be more specific?
By the way, in visual studio 7 the original program runs fine and there
is no problem with cross-thread operation. Is this a new feature of
Visual studio 8?
Thanks

Danny

Marc Gravell wrote:
Invoke and BeginInvoke work (IIRC) using the message pump, and this can only
happen once they have been created in the Win32 sense, and have Win32
handles.

This typically means you haven't shown your form yet, so Show() it;
alternatively, if you are subclassing Form (i.e. you have a MyForm.cs or
similar), then you might be able to get away with calling the protected
method CreateHandle(). Personally, I'd just Show() the form...

Does this help?

Marc
Oct 4 '06 #9
Application.Run() will indeed Show() the form, so this is fine.

When are you kicking off the worker? Is it perhaps in the constructor? In
which case, depending on timing it is possible that the worker could call
back before Application.Run() has time to Show() the form. If this is the
problem, try kicking off the worker in the Load event instead.

Marc
Oct 4 '06 #10
Marc,
As i am new to .NET programming, excuse me if I am not familiar with
the expression "kicking off the worker "

Danny

Marc Gravell wrote:
Application.Run() will indeed Show() the form, so this is fine.

When are you kicking off the worker? Is it perhaps in the constructor? In
which case, depending on timing it is possible that the worker could call
back before Application.Run() has time to Show() the form. If this is the
problem, try kicking off the worker in the Load event instead.

Marc
Oct 4 '06 #11
sorry, by "kicking off" I mean "starting", and by "worker" I mean "the other
thread in this situation"... i.e. where does the following (from you earlier
post) get called?

readThread = new Thread(new ThreadStart(RunServer));
readThread.Start();

Marc
Oct 4 '06 #12
Marc,
Indeed the call is from the constructor. I guess this is the problem.

Thanks a lot

Danny
Marc Gravell wrote:
sorry, by "kicking off" I mean "starting", and by "worker" I mean "the other
thread in this situation"... i.e. where does the following (from you earlier
post) get called?

readThread = new Thread(new ThreadStart(RunServer));
readThread.Start();

Marc
Oct 4 '06 #13
If you move it into a handler on the Load event, then it will get fired when
the form is shown.

Best of luck,

Marc
Oct 4 '06 #14
Marc,
I moved the lines
readThread = new Thread(new ThreadStart(RunServer));
readThread.Start();

from the form constructor into a button handler, and this solved the
original problem, but then somehow the KeyDown handler of a text box
doesn ot get fired. How come?
Thanks
Dani

Marc Gravell wrote:
If you move it into a handler on the Load event, then it will get fired when
the form is shown.

Best of luck,

Marc
Oct 8 '06 #15
Unless you have good reason to suggest otherwise, I reckon this is
unrelated. The only way it might be connected is if it *is* firing, but
hitting a sync lock.

Any example code (that reproduces without having to hack it)?

Marc

Oct 8 '06 #16

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

Similar topics

0
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
3
by: rollasoc | last post by:
Hi, Doing a bit of system testing on a Windows 98 laptop. (.Net 1.1 app). Did a bit of testing. Loaded a previously saved file. A gray box appeared with the text and buttons all white...
0
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
23
by: Jeff Rodriguez | last post by:
Here's what I want do: Have a main daemon which starts up several threads in a Boss-Queue structure. From those threads, I want them all to sit and watch a queue. Once an entry goes into the...
0
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
1
by: Rob Woodworth | last post by:
Hi, I'm having serious problems getting my report to work. I need to generate a timesheet report which will contain info for one employee between certain dates (one week's worth of dates). I...
6
by: Simon | last post by:
Hi All, An experiment i'm doing requires requires a synchronous cross-domain request, without using a proxy. I wondered if anyone had any ideas to help me achieve this. Below is what I have...
6
by: Bart Van der Donck | last post by:
Hello, I'm presenting my new library 'AJAX Cross Domain' - a javascript extension that allows to perform cross-domain AJAX requests. http://www.ajax-cross-domain.com/ Any comments or...
6
by: ampo | last post by:
Hello. Can anyone help with cross-domain problem? I have HTML page from server1 that send xmlHTTPRequest to server2. How can I do it? Thanks.
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
0
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,...
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.