thiago costa

thiago costa

  • NA
  • 319
  • 0

:: ERROR ::Cross-thread operation not valid: Control 'TB_an1' accessed from a thread other than the thread it was created on. While trying to read .txt file to textBox.Text

Oct 31 2011 7:44 PM
ERROR APPEARS ON THIS LINE:::::   
StreamReader streamreader1an1 = new StreamReader(@tempPath + "an1.txt"); TB_an1.Text = streamreader1an1.ReadToEnd(); streamreader1an1.Close();

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


I don't understand the error :'(

The program below is the program... 


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using System.Runtime.InteropServices;
using System.Threading;
using System.Timers;
using System.Net;
using System.IO;


namespace Ultra_Tournment
{
    public partial class Form1 : Form
    {
       
        private static System.Timers.Timer aTimer;
        private static System.Timers.Timer bTimer;


        public Form1()
        {
            InitializeComponent();
            string tempPath = System.IO.Path.GetTempPath();
            //
           


            bTimer = new System.Timers.Timer(10000);


            // Hook up the Elapsed event for the timer.
            bTimer.Elapsed += new ElapsedEventHandler(OnTimedEventZ);


            // Set the Interval to 2 seconds (2000 milliseconds).
            bTimer.Interval = 6000;
            bTimer.Enabled = true;




             aTimer = new System.Timers.Timer(10000);


            // Hook up the Elapsed event for the timer.
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);


            // Set the Interval to 2 seconds (2000 milliseconds).
            aTimer.Interval = 5000;
            aTimer.Enabled = true;
           
        }






        const int HT_CAPTION = 0x2;
        const int WM_NCLBUTTONDOWN = 0xA1;


        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();
           


           




        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            string tempPath = System.IO.Path.GetTempPath();
            //aNAME 1- 10                                                            
            WebClient Client = new WebClient();
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/an1.txt", @tempPath+"an1.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/an2.txt", @tempPath + "an2.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/an3.txt", @tempPath + "an3.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/an4.txt", @tempPath + "an4.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/an5.txt", @tempPath + "an5.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/an6.txt", @tempPath + "an6.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/an7.txt", @tempPath + "an7.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/an8.txt", @tempPath + "an8.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/an9.txt", @tempPath + "an9.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/an10.txt", @tempPath + "an10.txt");


            //bNAME 1-10
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bn1.txt", @tempPath + "bn1.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bn2.txt", @tempPath + "bn2.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bn3.txt", @tempPath + "bn3.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bn4.txt", @tempPath + "bn4.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bn5.txt", @tempPath + "bn5.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bn6.txt", @tempPath + "bn6.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bn7.txt", @tempPath + "bn7.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bn8.txt", @tempPath + "bn8.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bn9.txt", @tempPath + "bn9.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bn10.txt", @tempPath + "bn10.txt");


            //bqty 1- 10


            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq1.txt", @tempPath + "aq1.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq2.txt", @tempPath + "aq2.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq3.txt", @tempPath + "aq3.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq4.txt", @tempPath + "aq4.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq5.txt", @tempPath + "aq5.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq6.txt", @tempPath + "aq6.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq7.txt", @tempPath + "aq7.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq8.txt", @tempPath + "aq8.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq9.txt", @tempPath + "aq9.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq10.txt", @tempPath + "aq10.txt");


            //bqty 1-10


            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq1.txt", @tempPath + "bq1.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq2.txt", @tempPath + "bq2.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq3.txt", @tempPath + "bq3.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq4.txt", @tempPath + "bq4.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq5.txt", @tempPath + "bq5.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq6.txt", @tempPath + "bq6.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq7.txt", @tempPath + "bq7.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq8.txt", @tempPath + "bq8.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq9.txt", @tempPath + "bq9.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq10.txt", @tempPath + "bn10.txt");




            //bqty 1- 10


            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq1.txt", @tempPath + "aq1.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq2.txt", @tempPath + "aq2.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq3.txt", @tempPath + "aq3.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq4.txt", @tempPath + "aq4.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq5.txt", @tempPath + "aq5.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq6.txt", @tempPath + "aq6.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq7.txt", @tempPath + "aq7.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq8.txt", @tempPath + "aq8.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq9.txt", @tempPath + "aq9.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq10.txt", @tempPath + "aq10.txt");


            //bqty 1-10
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq1.txt", @tempPath + "bq1.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq2.txt", @tempPath + "bq2.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq3.txt", @tempPath + "bq3.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq4.txt", @tempPath + "bq4.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq5.txt", @tempPath + "bq5.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq6.txt", @tempPath + "bq6.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq7.txt", @tempPath + "bq7.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq8.txt", @tempPath + "bq8.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq9.txt", @tempPath + "bq9.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/bq10.txt", @tempPath + "bq10.txt");


            //bqty 1- 10


            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq1.txt", @tempPath + "alr1.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq2.txt", @tempPath + "alr2.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq3.txt", @tempPath + "alr3.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq4.txt", @tempPath + "alr4.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq5.txt", @tempPath + "alr5.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq6.txt", @tempPath + "alr6.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq7.txt", @tempPath + "alr7.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq8.txt", @tempPath + "alr8.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq9.txt", @tempPath + "alr9.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq10.txt", @tempPath + "alr10.txt");


            //bqty 1-10
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq1.txt", @tempPath + "blr1.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq2.txt", @tempPath + "blr2.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq3.txt", @tempPath + "blr3.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq4.txt", @tempPath + "blr4.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq5.txt", @tempPath + "blr5.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq6.txt", @tempPath + "blr6.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq7.txt", @tempPath + "blr7.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq8.txt", @tempPath + "blr8.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq9.txt", @tempPath + "blr9.txt");
            Client.DownloadFile("http://ultracomputers.info/dakkon/aposta/aq10.txt", @tempPath + "blr10.txt");
            Client.Dispose();
           




           




        }
        public void OnTimedEventZ(object source, ElapsedEventArgs e)
        {
            string tempPath = System.IO.Path.GetTempPath();
            //an's


            StreamReader streamreader1an1 = new StreamReader(@tempPath + "an1.txt"); TB_an1.Text = streamreader1an1.ReadToEnd(); streamreader1an1.Close();
            StreamReader streamreader2an1 = new StreamReader(@tempPath + "an2.txt"); TB_an2.Text = streamreader2an1.ReadToEnd(); streamreader2an1.Close();
            StreamReader streamreader3an1 = new StreamReader(@tempPath + "an3.txt"); TB_an3.Text = streamreader3an1.ReadToEnd(); streamreader3an1.Close();
            StreamReader streamreader4an1 = new StreamReader(@tempPath + "an4.txt"); TB_an4.Text = streamreader4an1.ReadToEnd(); streamreader4an1.Close();
            StreamReader streamreader5an1 = new StreamReader(@tempPath + "an5.txt"); TB_an5.Text = streamreader5an1.ReadToEnd(); streamreader5an1.Close();
            StreamReader streamreader6an1 = new StreamReader(@tempPath + "an6.txt"); TB_an6.Text = streamreader6an1.ReadToEnd(); streamreader6an1.Close();
            StreamReader streamreader7an1 = new StreamReader(@tempPath + "an7.txt"); TB_an7.Text = streamreader7an1.ReadToEnd(); streamreader7an1.Close();
            StreamReader streamreader8an1 = new StreamReader(@tempPath + "an8.txt"); TB_an8.Text = streamreader8an1.ReadToEnd(); streamreader8an1.Close();
            StreamReader streamreader9an1 = new StreamReader(@tempPath + "an9.txt"); TB_an9.Text = streamreader9an1.ReadToEnd(); streamreader9an1.Close();
            StreamReader streamreader10an1 = new StreamReader(@tempPath + "an10.txt"); TB_an10.Text = streamreader10an1.ReadToEnd(); streamreader10an1.Close();


            //bn's
            StreamReader streamreader1bn1 = new StreamReader(@tempPath + "bn1.txt"); TB_bn1.Text = streamreader1bn1.ReadToEnd(); streamreader1bn1.Close();
            StreamReader streamreader2bn1 = new StreamReader(@tempPath + "bn2.txt"); TB_bn2.Text = streamreader2bn1.ReadToEnd(); streamreader2bn1.Close();
            StreamReader streamreader3bn1 = new StreamReader(@tempPath + "bn3.txt"); TB_bn3.Text = streamreader3bn1.ReadToEnd(); streamreader3bn1.Close();
            StreamReader streamreader4bn1 = new StreamReader(@tempPath + "bn4.txt"); TB_bn4.Text = streamreader4bn1.ReadToEnd(); streamreader4bn1.Close();
            StreamReader streamreader5bn1 = new StreamReader(@tempPath + "bn5.txt"); TB_bn5.Text = streamreader5bn1.ReadToEnd(); streamreader5bn1.Close();
            StreamReader streamreader6bn1 = new StreamReader(@tempPath + "bn6.txt"); TB_bn6.Text = streamreader6bn1.ReadToEnd(); streamreader6bn1.Close();
            StreamReader streamreader7bn1 = new StreamReader(@tempPath + "bn7.txt"); TB_bn7.Text = streamreader7bn1.ReadToEnd(); streamreader7bn1.Close();
            StreamReader streamreader8bn1 = new StreamReader(@tempPath + "bn8.txt"); TB_bn8.Text = streamreader8bn1.ReadToEnd(); streamreader8bn1.Close();
            StreamReader streamreader9bn1 = new StreamReader(@tempPath + "bn9.txt"); TB_bn9.Text = streamreader9bn1.ReadToEnd(); streamreader9bn1.Close();
            StreamReader streamreader10bn1 = new StreamReader(@tempPath + "bn10.txt"); TB_bn10.Text = streamreader10bn1.ReadToEnd(); streamreader10bn1.Close();


            //aqts
            StreamReader streamreader1aq1 = new StreamReader(@tempPath + "aq1.txt"); TB_aq1.Text = streamreader1aq1.ReadToEnd(); streamreader1aq1.Close();
            StreamReader streamreader2aq1 = new StreamReader(@tempPath + "aq2.txt"); TB_aq2.Text = streamreader2aq1.ReadToEnd(); streamreader2aq1.Close();
            StreamReader streamreader3aq1 = new StreamReader(@tempPath + "aq3.txt"); TB_aq3.Text = streamreader3aq1.ReadToEnd(); streamreader3aq1.Close();
            StreamReader streamreader4aq1 = new StreamReader(@tempPath + "aq4.txt"); TB_aq4.Text = streamreader4aq1.ReadToEnd(); streamreader4aq1.Close();
            StreamReader streamreader5aq1 = new StreamReader(@tempPath + "aq5.txt"); TB_aq5.Text = streamreader5aq1.ReadToEnd(); streamreader5aq1.Close();
            StreamReader streamreader6aq1 = new StreamReader(@tempPath + "aq6.txt"); TB_aq6.Text = streamreader6aq1.ReadToEnd(); streamreader6aq1.Close();
            StreamReader streamreader7aq1 = new StreamReader(@tempPath + "aq7.txt"); TB_aq7.Text = streamreader7aq1.ReadToEnd(); streamreader7aq1.Close();
            StreamReader streamreader8aq1 = new StreamReader(@tempPath + "aq8.txt"); TB_aq8.Text = streamreader8aq1.ReadToEnd(); streamreader8aq1.Close();
            StreamReader streamreader9aq1 = new StreamReader(@tempPath + "aq9.txt"); TB_aq9.Text = streamreader9aq1.ReadToEnd(); streamreader9aq1.Close();
            StreamReader streamreader10aq1 = new StreamReader(@tempPath + "aq10.txt"); TB_aq10.Text = streamreader10aq1.ReadToEnd(); streamreader10aq1.Close();


            //bqts
            StreamReader streamreader1bq1 = new StreamReader(@tempPath + "bq1.txt"); TB_bq1.Text = streamreader1bq1.ReadToEnd(); streamreader1bq1.Close();
            StreamReader streamreader2bq1 = new StreamReader(@tempPath + "bq2.txt"); TB_bq2.Text = streamreader2bq1.ReadToEnd(); streamreader2bq1.Close();
            StreamReader streamreader3bq1 = new StreamReader(@tempPath + "bq3.txt"); TB_bq3.Text = streamreader3bq1.ReadToEnd(); streamreader3bq1.Close();
            StreamReader streamreader4bq1 = new StreamReader(@tempPath + "bq4.txt"); TB_bq4.Text = streamreader4bq1.ReadToEnd(); streamreader4bq1.Close();
            StreamReader streamreader5bq1 = new StreamReader(@tempPath + "bq5.txt"); TB_bq5.Text = streamreader5bq1.ReadToEnd(); streamreader5bq1.Close();
            StreamReader streamreader6bq1 = new StreamReader(@tempPath + "bq6.txt"); TB_bq6.Text = streamreader6bq1.ReadToEnd(); streamreader6bq1.Close();
            StreamReader streamreader7bq1 = new StreamReader(@tempPath + "bq7.txt"); TB_bq7.Text = streamreader7bq1.ReadToEnd(); streamreader7bq1.Close();
            StreamReader streamreader8bq1 = new StreamReader(@tempPath + "bq8.txt"); TB_bq8.Text = streamreader8bq1.ReadToEnd(); streamreader8bq1.Close();
            StreamReader streamreader9bq1 = new StreamReader(@tempPath + "bq9.txt"); TB_bq9.Text = streamreader9bq1.ReadToEnd(); streamreader9bq1.Close();
            StreamReader streamreader10bq1 = new StreamReader(@tempPath + "bq10.txt"); TB_bq10.Text = streamreader10bq1.ReadToEnd(); streamreader10bq1.Close();




            //arl
            StreamReader streamreader1alr1 = new StreamReader(@tempPath + "alr1.txt"); TB_alr1.Text = streamreader1alr1.ReadToEnd(); streamreader1alr1.Close();
            StreamReader streamreader2alr1 = new StreamReader(@tempPath + "alr2.txt"); TB_alr2.Text = streamreader2alr1.ReadToEnd(); streamreader2alr1.Close();
            StreamReader streamreader3alr1 = new StreamReader(@tempPath + "alr3.txt"); TB_alr3.Text = streamreader3alr1.ReadToEnd(); streamreader3alr1.Close();
            StreamReader streamreader4alr1 = new StreamReader(@tempPath + "alr4.txt"); TB_alr4.Text = streamreader4alr1.ReadToEnd(); streamreader4alr1.Close();
            StreamReader streamreader5alr1 = new StreamReader(@tempPath + "alr5.txt"); TB_alr5.Text = streamreader5alr1.ReadToEnd(); streamreader5alr1.Close();
            StreamReader streamreader6alr1 = new StreamReader(@tempPath + "alr6.txt"); TB_alr6.Text = streamreader6alr1.ReadToEnd(); streamreader6alr1.Close();
            StreamReader streamreader7alr1 = new StreamReader(@tempPath + "alr7.txt"); TB_alr7.Text = streamreader7alr1.ReadToEnd(); streamreader7alr1.Close();
            StreamReader streamreader8alr1 = new StreamReader(@tempPath + "alr8.txt"); TB_alr8.Text = streamreader8alr1.ReadToEnd(); streamreader8alr1.Close();
            StreamReader streamreader9alr1 = new StreamReader(@tempPath + "alr9.txt"); TB_alr9.Text = streamreader9alr1.ReadToEnd(); streamreader9alr1.Close();
            StreamReader streamreader10alr1 = new StreamReader(@tempPath + "alr10.txt"); TB_alr10.Text = streamreader10alr1.ReadToEnd(); streamreader10alr1.Close();


            //brl


            StreamReader streamreader1blr1 = new StreamReader(@tempPath + "blr1.txt"); TB_blr1.Text = streamreader1blr1.ReadToEnd(); streamreader1blr1.Close();
            StreamReader streamreader2blr1 = new StreamReader(@tempPath + "blr2.txt"); TB_blr2.Text = streamreader2blr1.ReadToEnd(); streamreader2blr1.Close();
            StreamReader streamreader3blr1 = new StreamReader(@tempPath + "blr3.txt"); TB_blr3.Text = streamreader3blr1.ReadToEnd(); streamreader3blr1.Close();
            StreamReader streamreader4blr1 = new StreamReader(@tempPath + "blr4.txt"); TB_blr4.Text = streamreader4blr1.ReadToEnd(); streamreader4blr1.Close();
            StreamReader streamreader5blr1 = new StreamReader(@tempPath + "blr5.txt"); TB_blr5.Text = streamreader5blr1.ReadToEnd(); streamreader5blr1.Close();
            StreamReader streamreader6blr1 = new StreamReader(@tempPath + "blr6.txt"); TB_blr6.Text = streamreader6blr1.ReadToEnd(); streamreader6blr1.Close();
            StreamReader streamreader7blr1 = new StreamReader(@tempPath + "blr7.txt"); TB_blr7.Text = streamreader7blr1.ReadToEnd(); streamreader7blr1.Close();
            StreamReader streamreader8blr1 = new StreamReader(@tempPath + "blr8.txt"); TB_blr8.Text = streamreader8blr1.ReadToEnd(); streamreader8blr1.Close();
            StreamReader streamreader9blr1 = new StreamReader(@tempPath + "blr9.txt"); TB_blr9.Text = streamreader9blr1.ReadToEnd(); streamreader9blr1.Close();
            StreamReader streamreader10blr1 = new StreamReader(@tempPath + "blr10.txt"); TB_blr10.Text = streamreader10blr1.ReadToEnd(); streamreader10blr1.Close();


        }


       


        private void Form1_Load(object sender, EventArgs e)
        {
           
        }


        private void TB_an1_TextChanged(object sender, EventArgs e)
        {


        }


        private void TB_blr10_TextChanged(object sender, EventArgs e)
        {


        }


       
    }
}



I posted the line that has the errors, in the beggining...

I dont understand... I want to be able to read line to textbox, after it downloaded it, did I do something wrong ?



Answers (4)