crossthreading issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Bullock

    #1

    crossthreading issue

    Hi, I've got a wierd problem...

    Here is my code:

    public partial class frmMain : Form
    {
    private MYClass myClass;

    private void btnRun_Click(ob ject sender, EventArgs e)
    {
    myClass = new MYClass(new CallBack(doStuf f));
    Thread t1 = new Thread(new ThreadStart(myC lass.Run));
    t1.Start();
    }

    private void doStuff()
    {
    pictureBox1.Ima ge = myClass.getPict ure();

    }
    }

    ///////////////////////////////////////

    public delegate void CallBack();
    class MYClass
    {
    private CallBack CallBackMethod;

    public MYClass(CallBac k cb)
    {
    CallBackMethod = cb;
    }
    public void Run()
    {
    // do something useful
    CallBackMethod( );
    }
    public Bitmap getPicture()
    {
    // return an img
    }
    }
    ///////////////////////////////////////////////////////////////////


    in doStuff() i grab data back from myClass like this:

    myClass.getData ();

    and then display it in pictureboxes, listboxes etc in the normal way
    (not using invoke - because i thought the point in callbacks was to
    avoid invoke). SOMETIMES (ive not worked out when/why) I get a
    cross-threading error:

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

    When the debugger takes me to break mode to display the error, if i just
    click the run button again, it continues fine :s Sometimes this error
    does not occur at all.

    Can anyone tell me why, and then what I can do about it?


    Thanks
    Andrew

  • Michael Nemtsev

    #2
    RE: crossthreading issue

    You shouldn't call doStuff directly if u are not in UI thread. Use *Invoke
    for this reason

    Detailed explanation is here

    [color=blue]
    > Hi, I've got a wierd problem...
    >
    > Here is my code:
    >
    > public partial class frmMain : Form
    > {
    > private MYClass myClass;
    >
    > private void btnRun_Click(ob ject sender, EventArgs e)
    > {
    > myClass = new MYClass(new CallBack(doStuf f));
    > Thread t1 = new Thread(new ThreadStart(myC lass.Run));
    > t1.Start();
    > }
    >
    > private void doStuff()
    > {
    > pictureBox1.Ima ge = myClass.getPict ure();
    >
    > }
    > }
    >
    > ///////////////////////////////////////
    >
    > public delegate void CallBack();
    > class MYClass
    > {
    > private CallBack CallBackMethod;
    >
    > public MYClass(CallBac k cb)
    > {
    > CallBackMethod = cb;
    > }
    > public void Run()
    > {
    > // do something useful
    > CallBackMethod( );
    > }
    > public Bitmap getPicture()
    > {
    > // return an img
    > }
    > }
    > ///////////////////////////////////////////////////////////////////
    >
    >
    > in doStuff() i grab data back from myClass like this:
    >
    > myClass.getData ();
    >
    > and then display it in pictureboxes, listboxes etc in the normal way
    > (not using invoke - because i thought the point in callbacks was to
    > avoid invoke). SOMETIMES (ive not worked out when/why) I get a
    > cross-threading error:
    >
    > "Cross-thread operation not valid: Control '' accessed from a thread
    > other than the thread it was created on."
    >
    > When the debugger takes me to break mode to display the error, if i just
    > click the run button again, it continues fine :s Sometimes this error
    > does not occur at all.
    >
    > Can anyone tell me why, and then what I can do about it?[/color]

    --
    WBR,
    Michael Nemtsev :: blog: http://spaces.msn.com/laflour

    "At times one remains faithful to a cause only because its opponents do not
    cease to be insipid." (c) Friedrich Nietzsche

    Comment

    • Stoitcho Goutsev \(100\)

      #3
      Re: crossthreading issue

      Andrew,

      Callbacks doesn't avoid using Control.Invoke. Calling a callback is the same
      as calling a method directly - it executes in the context of the calling
      thread. If you use .NET 2.0 and ther BackgroundWorke r than its Events
      ProgressChanged and RunWorkerComple te are executed in the UI thread so you
      don't need to call Control.Invoke from within these event hadnlers.


      --
      HTH
      Stoitcho Goutsev (100)

      "Andrew Bullock" <andrewREMOVEbu llockTHIS@ANDnt lworldTHIS.com> wrote in
      message news:7xZKf.4353 0$Dn4.18786@new sfe3-gui.ntli.net...[color=blue]
      > Hi, I've got a wierd problem...
      >
      > Here is my code:
      >
      > public partial class frmMain : Form
      > {
      > private MYClass myClass;
      >
      > private void btnRun_Click(ob ject sender, EventArgs e)
      > {
      > myClass = new MYClass(new CallBack(doStuf f));
      > Thread t1 = new Thread(new ThreadStart(myC lass.Run));
      > t1.Start();
      > }
      >
      > private void doStuff()
      > {
      > pictureBox1.Ima ge = myClass.getPict ure();
      >
      > }
      > }
      >
      > ///////////////////////////////////////
      >
      > public delegate void CallBack();
      > class MYClass
      > {
      > private CallBack CallBackMethod;
      >
      > public MYClass(CallBac k cb)
      > {
      > CallBackMethod = cb;
      > }
      > public void Run()
      > {
      > // do something useful
      > CallBackMethod( );
      > }
      > public Bitmap getPicture()
      > {
      > // return an img
      > }
      > }
      > ///////////////////////////////////////////////////////////////////
      >
      >
      > in doStuff() i grab data back from myClass like this:
      >
      > myClass.getData ();
      >
      > and then display it in pictureboxes, listboxes etc in the normal way
      > (not using invoke - because i thought the point in callbacks was to
      > avoid invoke). SOMETIMES (ive not worked out when/why) I get a
      > cross-threading error:
      >
      > "Cross-thread operation not valid: Control '' accessed from a thread other
      > than the thread it was created on."
      >
      > When the debugger takes me to break mode to display the error, if i just
      > click the run button again, it continues fine :s Sometimes this error does
      > not occur at all.
      >
      > Can anyone tell me why, and then what I can do about it?
      >
      >
      > Thanks
      > Andrew
      >[/color]


      Comment

      • Willy Denoyette [MVP]

        #4
        Re: crossthreading issue


        "Andrew Bullock" <andrewREMOVEbu llockTHIS@ANDnt lworldTHIS.com> wrote in
        message news:7xZKf.4353 0$Dn4.18786@new sfe3-gui.ntli.net...
        | Hi, I've got a wierd problem...
        |
        | Here is my code:
        |
        | public partial class frmMain : Form
        | {
        | private MYClass myClass;
        |
        | private void btnRun_Click(ob ject sender, EventArgs e)
        | {
        | myClass = new MYClass(new CallBack(doStuf f));
        | Thread t1 = new Thread(new ThreadStart(myC lass.Run));
        | t1.Start();
        | }
        |
        | private void doStuff()
        | {
        | pictureBox1.Ima ge = myClass.getPict ure();
        |
        | }
        | }
        |
        | ///////////////////////////////////////
        |
        | public delegate void CallBack();
        | class MYClass
        | {
        | private CallBack CallBackMethod;
        |
        | public MYClass(CallBac k cb)
        | {
        | CallBackMethod = cb;
        | }
        | public void Run()
        | {
        | // do something useful
        | CallBackMethod( );
        | }
        | public Bitmap getPicture()
        | {
        | // return an img
        | }
        | }
        | ///////////////////////////////////////////////////////////////////
        |
        |
        | in doStuff() i grab data back from myClass like this:
        |
        | myClass.getData ();
        |
        | and then display it in pictureboxes, listboxes etc in the normal way
        | (not using invoke - because i thought the point in callbacks was to
        | avoid invoke). SOMETIMES (ive not worked out when/why) I get a
        | cross-threading error:
        |
        | "Cross-thread operation not valid: Control '' accessed from a thread
        | other than the thread it was created on."
        |
        | When the debugger takes me to break mode to display the error, if i just
        | click the run button again, it continues fine :s Sometimes this error
        | does not occur at all.
        |
        | Can anyone tell me why, and then what I can do about it?
        |
        |
        | Thanks
        | Andrew
        |

        Just to add what Michael and Stoitcho said, there is no reason to run this
        on a separate thread, the action "getPicture " only takes a few milliseconds
        to complete, so you won't freeze the UI thread at all.

        Willy.


        Comment

        • Andrew Bullock

          #5
          Re: crossthreading issue

          Stoitcho Goutsev (100) wrote:[color=blue]
          > Andrew,
          >
          > Callbacks doesn't avoid using Control.Invoke. Calling a callback is the same
          > as calling a method directly - it executes in the context of the calling
          > thread. If you use .NET 2.0 and ther BackgroundWorke r than its Events
          > ProgressChanged and RunWorkerComple te are executed in the UI thread so you
          > don't need to call Control.Invoke from within these event hadnlers.
          >
          >[/color]


          Ace, thanks for that :)

          Andrew

          Comment

          Working...