View Single Post
  #1 (permalink)  
Old July 27th, 2007, 02:25 PM
angelboy angelboy is offline
Friend of Wrox
 
Join Date: Dec 2006
Location: , , .
Posts: 176
Thanks: 0
Thanked 0 Times in 0 Posts
Default cross thread Error

hi expert
i use the flowing code to read the file into textBox

my code is

Code:

public partial class Form1 : Form
    {
        byte[] fileContent;
        AsyncCallback callBack;
        FileStream fs;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAsync_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            if (op.ShowDialog() == DialogResult.OK)
            {

                fs = new FileStream(op.FileName, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true);
                callBack = new AsyncCallback(fs_ReadAsync);
                fileContent = new byte[fs.Length];
                fs.BeginRead(fileContent, 0, (int)fs.Length, callBack, null);


            }

        }

        private void fs_ReadAsync(IAsyncResult Result)
        {


            if (Result.IsCompleted)
            {


                txtResult.Text = Encoding.UTF8.GetString(fileContent);
                fs.Close();
            }
        }



    }
but when i run the program and i press the brnAsync and choose my file i receive the flowing error


Code:

Cross-thread operation not valid:
 Control 'txtResult' accessed from a thread other than the thread it was created on.
help me
regards