Click here to Skip to main content
15,900,816 members
Home / Discussions / C#
   

C#

 
GeneralRe: So Many ADO Data Providers Which Should I Use? Pin
redfish3413-Sep-06 17:50
redfish3413-Sep-06 17:50 
QuestionSetup doesnt generate reg keys Pin
Gutec13-Sep-06 10:48
Gutec13-Sep-06 10:48 
QuestionPrint page with out a footer. Pin
spark12800013-Sep-06 10:10
spark12800013-Sep-06 10:10 
QuestionGet domain name from email address Pin
MyRunner2313-Sep-06 9:34
MyRunner2313-Sep-06 9:34 
AnswerRe: Get domain name from email address Pin
User 665813-Sep-06 9:45
User 665813-Sep-06 9:45 
GeneralRe: Get domain name from email address Pin
MyRunner2313-Sep-06 10:00
MyRunner2313-Sep-06 10:00 
AnswerRe: Get domain name from email address Pin
spark12800013-Sep-06 10:21
spark12800013-Sep-06 10:21 
Questiona problem with threads. Pin
Green Fuze13-Sep-06 9:10
Green Fuze13-Sep-06 9:10 
Hi Everybody!

Man, what would I do without this board ?! Smile | :) I think everything I know in programming is because of everybody's help in this board and code project's articles! THANKS! Big Grin | :-D (REALLY!)

anyway, enough being emotional and all Smile | :) .

I created a thread that receives a stream from the net to get a string.
I want that string to be displayed on the application textbox (or in other words, instant messanger).
The problem is that when I'm trying to assign StreamReader's text into textbox.text I'm getting exception saying that:

"Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on."

I REALLY don't understand why is that. is it because textbox1 is on ONE thread (the window form thread) and the StreamReader is on the other (the thread I created)?
if so, how can I fix it?!?!?! Confused | :confused:

That is the code of the service() method ran by the thread:
<br />
private void service()<br />
        {<br />
            while(true)<br />
            {<br />
                // get message from the socket.<br />
                socket = listener.AcceptSocket();<br />
                <br />
                try<br />
                {<br />
                    // create a stream object to get information from socket.<br />
                    Stream stream = new NetworkStream(socket);<br />
<br />
                    // streamreader to read from the socket<br />
                    StreamReader sr = new StreamReader(stream);<br />
<br />
                    // streamwriter to write to the socket<br />
                    StreamWriter sw = new StreamWriter(stream);<br />
                    sw.AutoFlush = true;<br />
<br />
                    string text = sr.ReadToEnd();<br />
                    this.textBox1.Text = text;<br />
<br />
                    stream.Close();<br />
                }<br />
                catch(Exception e)<br />
                {<br />
<br />
                    MessageBox.Show(e.Message);<br />
<br />
                }<br />
<br />
                socket.Close();<br />
            }<br />
        }



Thanks!!!!
AnswerRe: a problem with threads. Pin
Stefan Troschuetz13-Sep-06 9:20
Stefan Troschuetz13-Sep-06 9:20 
GeneralRe: a problem with threads. Pin
Green Fuze13-Sep-06 9:25
Green Fuze13-Sep-06 9:25 
JokeRe: a problem with threads. Pin
Guffa13-Sep-06 11:30
Guffa13-Sep-06 11:30 
QuestionQuick generics q Pin
thepersonof13-Sep-06 8:26
thepersonof13-Sep-06 8:26 
AnswerRe: Quick generics q Pin
Rob Graham13-Sep-06 8:50
Rob Graham13-Sep-06 8:50 
GeneralRe: Quick generics q Pin
thepersonof13-Sep-06 9:47
thepersonof13-Sep-06 9:47 
GeneralRe: Quick generics q Pin
Nader Elshehabi13-Sep-06 10:35
Nader Elshehabi13-Sep-06 10:35 
Questionimplementing drag drop ability with figures Pin
ssoffline13-Sep-06 8:16
ssoffline13-Sep-06 8:16 
AnswerRe: implementing drag drop ability with figures Pin
Nader Elshehabi13-Sep-06 9:27
Nader Elshehabi13-Sep-06 9:27 
Questionreport viewer Pin
TAREQ F ABUZUHRI13-Sep-06 7:54
TAREQ F ABUZUHRI13-Sep-06 7:54 
QuestionGet the width of a string in C# without Graphics Pin
Goebel13-Sep-06 7:48
Goebel13-Sep-06 7:48 
AnswerRe: Get the width of a string in C# without Graphics Pin
Nader Elshehabi13-Sep-06 10:28
Nader Elshehabi13-Sep-06 10:28 
GeneralRe: Get the width of a string in C# without Graphics Pin
Goebel13-Sep-06 11:07
Goebel13-Sep-06 11:07 
GeneralRe: Get the width of a string in C# without Graphics Pin
Nader Elshehabi13-Sep-06 11:26
Nader Elshehabi13-Sep-06 11:26 
GeneralRe: Get the width of a string in C# without Graphics Pin
Goebel13-Sep-06 12:16
Goebel13-Sep-06 12:16 
GeneralRe: Get the width of a string in C# without Graphics Pin
Nader Elshehabi13-Sep-06 12:41
Nader Elshehabi13-Sep-06 12:41 
GeneralRe: Get the width of a string in C# without Graphics Pin
Goebel13-Sep-06 21:59
Goebel13-Sep-06 21:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.