Thread problem ? 
Author Message
 Thread problem ?

Lo,

private void button1_Click(object sender, System.EventArgs e)

{

Thread t1 = new Thread(new ThreadStart(tAffiche1));

Thread t2 = new Thread(new ThreadStart(tAffiche2));

t1.Start();

t2.Start();

Quote:
}

private void tAffiche1()

{

for(long i=0; i<9999999999; i++)

{

richTextBox1.SelectionStart = richTextBox1.TextLength;

richTextBox1.SelectionLength = 0;

richTextBox1.SelectionColor = Color.Red;

richTextBox1.AppendText("Hello");

Quote:
}
}

private void tAffiche2()

{

for(long i=0; i<9999999999; i++)

{

richTextBox1.SelectionStart = richTextBox1.TextLength;

richTextBox1.SelectionLength = 0;

richTextBox1.SelectionColor = Color.Blue;

richTextBox1.AppendText("Bye");

Quote:
}
}

Why does it make an exception ? Is it a thread problem, if yea how to solve
it ?

Thanks a lot



Thu, 30 Dec 2004 22:57:09 GMT  
 Thread problem ?
(Disclaimer: I'm not a very experienced windows forms programmer,
so I may be wrong, but...)

From what I've heard, you shouldn't access controls from
anything other than the main UI thread.

-c


Quote:
> Lo,

> private void button1_Click(object sender, System.EventArgs e)

> {

> Thread t1 = new Thread(new ThreadStart(tAffiche1));

> Thread t2 = new Thread(new ThreadStart(tAffiche2));

> t1.Start();

> t2.Start();

> }

> private void tAffiche1()

> {

> for(long i=0; i<9999999999; i++)

> {

> richTextBox1.SelectionStart = richTextBox1.TextLength;

> richTextBox1.SelectionLength = 0;

> richTextBox1.SelectionColor = Color.Red;

> richTextBox1.AppendText("Hello");

> }

> }

> private void tAffiche2()

> {

> for(long i=0; i<9999999999; i++)

> {

> richTextBox1.SelectionStart = richTextBox1.TextLength;

> richTextBox1.SelectionLength = 0;

> richTextBox1.SelectionColor = Color.Blue;

> richTextBox1.AppendText("Bye");

> }

> }

> Why does it make an exception ? Is it a thread problem, if yea how to
solve
> it ?

> Thanks a lot



Thu, 30 Dec 2004 23:26:02 GMT  
 Thread problem ?
You can't directly call controls from other threads...
read
  http://msdn.microsoft.com/library/en-us/cpguide/html/cpcondevelopingm...

also note there is
 Control.InvokeRequired
 http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindows...

--
  NETMaster  (Thomas Scheidegger)
  http://www.cetus-links.org/oo_CSharp.html

Quote:

> private void button1_Click(object sender, System.EventArgs e)
> Thread t1 = new Thread(new ThreadStart(tAffiche1));
> Why does it make an exception ? Is it a thread problem, if yea how to solve it ?



Fri, 31 Dec 2004 00:11:34 GMT  
 Thread problem ?
I've tried with Invoke but it still makes the exception ;(((


Quote:
> (Disclaimer: I'm not a very experienced windows forms programmer,
> so I may be wrong, but...)

> From what I've heard, you shouldn't access controls from
> anything other than the main UI thread.

> -c



> > Lo,

> > private void button1_Click(object sender, System.EventArgs e)

> > {

> > Thread t1 = new Thread(new ThreadStart(tAffiche1));

> > Thread t2 = new Thread(new ThreadStart(tAffiche2));

> > t1.Start();

> > t2.Start();

> > }

> > private void tAffiche1()

> > {

> > for(long i=0; i<9999999999; i++)

> > {

> > richTextBox1.SelectionStart = richTextBox1.TextLength;

> > richTextBox1.SelectionLength = 0;

> > richTextBox1.SelectionColor = Color.Red;

> > richTextBox1.AppendText("Hello");

> > }

> > }

> > private void tAffiche2()

> > {

> > for(long i=0; i<9999999999; i++)

> > {

> > richTextBox1.SelectionStart = richTextBox1.TextLength;

> > richTextBox1.SelectionLength = 0;

> > richTextBox1.SelectionColor = Color.Blue;

> > richTextBox1.AppendText("Bye");

> > }

> > }

> > Why does it make an exception ? Is it a thread problem, if yea how to
> solve
> > it ?

> > Thanks a lot



Fri, 31 Dec 2004 00:13:07 GMT  
 Thread problem ?
{*filter*}ik,

    You will need to call the invoke method from the secondary thread.  Do
this:

// The code that formats your text.
public void FormatTextBox(object sender, EventArgs e)
{
    // Append the text to the textbox.
    richTextBox1.SelectionStart = richTextBox1.TextLength;
    richTextBox1.SelectionLength = 0;
    richTextBox1.SelectionColor = Color.Red;
    richTextBox1.AppendText("Hello");

Quote:
}

// Now, in your threads, do this:
private void tAffiche1()
{
    for(long i = 0; i < 9999999999; i++)
    {
        // Call invoke on the thread richTextBox1 was created on.
        richTextBox1.Invoke(new EventHandler(this.FormatTextBox), new
object[]{null, null});
    }

Quote:
}

private void tAffiche2()
{
    for(long i = 0; i < 9999999999; i++)
    {
        // Call invoke on the thread richTextBox1 was created on.
        richTextBox1.Invoke(new EventHandler(this.FormatTextBox), new
object[]{null, null});
    }

Quote:
}

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> Lo,

> private void button1_Click(object sender, System.EventArgs e)

> {

> Thread t1 = new Thread(new ThreadStart(tAffiche1));

> Thread t2 = new Thread(new ThreadStart(tAffiche2));

> t1.Start();

> t2.Start();

> }

> private void tAffiche1()

> {

> for(long i=0; i<9999999999; i++)

> {

> richTextBox1.SelectionStart = richTextBox1.TextLength;

> richTextBox1.SelectionLength = 0;

> richTextBox1.SelectionColor = Color.Red;

> richTextBox1.AppendText("Hello");

> }

> }

> private void tAffiche2()

> {

> for(long i=0; i<9999999999; i++)

> {

> richTextBox1.SelectionStart = richTextBox1.TextLength;

> richTextBox1.SelectionLength = 0;

> richTextBox1.SelectionColor = Color.Blue;

> richTextBox1.AppendText("Bye");

> }

> }

> Why does it make an exception ? Is it a thread problem, if yea how to
solve
> it ?

> Thanks a lot



Fri, 31 Dec 2004 22:08:07 GMT  
 Thread problem ?
I'll try this thanks !!


Quote:
> {*filter*}ik,

>     You will need to call the invoke method from the secondary thread.  Do
> this:

> // The code that formats your text.
> public void FormatTextBox(object sender, EventArgs e)
> {
>     // Append the text to the textbox.
>     richTextBox1.SelectionStart = richTextBox1.TextLength;
>     richTextBox1.SelectionLength = 0;
>     richTextBox1.SelectionColor = Color.Red;
>     richTextBox1.AppendText("Hello");
> }

> // Now, in your threads, do this:
> private void tAffiche1()
> {
>     for(long i = 0; i < 9999999999; i++)
>     {
>         // Call invoke on the thread richTextBox1 was created on.
>         richTextBox1.Invoke(new EventHandler(this.FormatTextBox), new
> object[]{null, null});
>     }
> }

> private void tAffiche2()
> {
>     for(long i = 0; i < 9999999999; i++)
>     {
>         // Call invoke on the thread richTextBox1 was created on.
>         richTextBox1.Invoke(new EventHandler(this.FormatTextBox), new
> object[]{null, null});
>     }
> }

>     Hope this helps.

> --
>                - Nicholas Paldino [.NET MVP]



> > Lo,

> > private void button1_Click(object sender, System.EventArgs e)

> > {

> > Thread t1 = new Thread(new ThreadStart(tAffiche1));

> > Thread t2 = new Thread(new ThreadStart(tAffiche2));

> > t1.Start();

> > t2.Start();

> > }

> > private void tAffiche1()

> > {

> > for(long i=0; i<9999999999; i++)

> > {

> > richTextBox1.SelectionStart = richTextBox1.TextLength;

> > richTextBox1.SelectionLength = 0;

> > richTextBox1.SelectionColor = Color.Red;

> > richTextBox1.AppendText("Hello");

> > }

> > }

> > private void tAffiche2()

> > {

> > for(long i=0; i<9999999999; i++)

> > {

> > richTextBox1.SelectionStart = richTextBox1.TextLength;

> > richTextBox1.SelectionLength = 0;

> > richTextBox1.SelectionColor = Color.Blue;

> > richTextBox1.AppendText("Bye");

> > }

> > }

> > Why does it make an exception ? Is it a thread problem, if yea how to
> solve
> > it ?

> > Thanks a lot



Fri, 31 Dec 2004 23:31:47 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Threading problem. I'm doing something wrong

2. MTA Thread Problem

3. Thread problem

4. C & thread problem

5. Thread Problem

6. threading problems

7. Exe server and threading problems

8. Threading problem in ATL COM object

9. Threading problem in ATL COM Object

10. VC++/ATL COM ASP component thread problem.

11. ISAPI dll DB thread problem

12. ATL threading problems

 

 
Powered by phpBB® Forum Software