CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2010
    Posts
    4

    Question recursive function question

    Hello you All;

    I have a recursive function which makes the UI freeze in my program.I would like to put this function in another thread however, because there are a lot of control connected with main form, whenever I attempt to run it VC# gives me a cross thread error which is I am dealing with right now.

    Also the function uses a structure which is on another class.

    I have tried to call my radiobuttons using the code I wrote below, but I am not sure this is the correct way to do this..

    Please help me out.. Thanks in advance
    [
    delegate void SetButtonCallback(RadioButton WhichOne, bool button);
    private void SetButton(RadioButton WhichOne, bool button)
    {
    if (WhichOne.InvokeRequired)
    {
    SetButtonCallback d = new SetButtonCallback(SetButton);
    this.BeginInvoke(d, new object[] { WhichOne, button });
    }
    else
    {
    WhichOne.Checked = button;
    }
    }
    ]

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: recursive function question

    I have a recursive function which makes the UI freeze in my program.
    First thing to do is to check if the recursive function is actually working correctly (i.e. it terminates at some point). You can do that by writing test program (maybe a console application or do a unit test using NUnit or VSTS).
    I would like to put this function in another thread however, because there are a lot of control connected with main form, whenever I attempt to run it VC# gives me a cross thread error which is I am dealing with right now.
    The error you are getting is because you are trying to change the state of controls which run in the UI thread from some other thread. I don't think it has to do with amount of controls on the screen. The solution is to define a method in the UI class that will do the update. When the background thread has finished doing its work you invoke this method. There are examples out there and on MSDN. I'm sure someone will give you one soon.
    Also the function uses a structure which is on another class.
    This shouldn't make any difference...
    I have tried to call my radiobuttons using the code I wrote below, but I am not sure this is the correct way to do this..
    Not sure what you're trying to do here...

    Also please use code tags when you post code in the forum. You can actually edit your original post to format your code. It helps others read and understand it and hopefully help you.

  3. #3
    Join Date
    Jan 2010
    Posts
    4

    Re: recursive function question

    thanks for your reply..

    if it is possible can you show me how to call functions from another thread.
    Because I get a lot of cross thread errors, and I still couldn't figure out how to solve them..

    Thanks

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured