Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
| Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 27th, 2007, 02:25 PM
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
  #2 (permalink)  
Old July 28th, 2007, 04:35 AM
Authorized User
 
Join Date: Sep 2004
Location: Edegem, Antwerpen, Belgium.
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

This is because you are trying to update the value of textbox which is hosted in a different thread. It is not possible to change the value of a control from a different thread.

To update the control from a different thread you need to use the invoke method. With this method you can call a method on the main thread that allows to update the control.

Here is a sample:

private void fs_ReadAsync(IAsyncResult Result)
{
    FileStream fs = (FileStream)result.AsyncState;
    fs.EndRead(result);
    fs.Close();

    string text = Encoding.UTF8.GetString(buffer);

    SetTextboxText(text);
}

delegate void SetTextboxTextDelegate(string newText);

void SetTextboxText(string newText)
{
    if (this.InvokeRequired)
        this.Invoke(new SetTextboxTextDelegate(this.SetTextboxText), new
        object[] { newText });
    else
        txtResult.Text = newText;
}

The InvokeRequired checks whether the control is accessible from the current thread. If not, you need to call the Invoke method and pass an instance of a delegate. The Invoke method then executes the method in the delegate on the same thread as the control's thread.

Another possibility is to use the BackGroundWorker (see http://geertverhoeven.blogspot.com/2...undworker.html)

Greetz,

Geert



http://geertverhoeven.blogspot.com


Similar Threads
Thread Thread Starter Forum Replies Last Post
cross thread exception YoungLuke C# 1 March 23rd, 2008 06:46 PM
cross thread exception with windows live API YoungLuke C# 2008 aka C# 3.0 1 March 23rd, 2008 01:43 PM
Cross-thread operation not valid Richw71 C# 0 December 13th, 2006 09:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.