Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:53 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.