Detection of thread executing

I have read up some on utilizing threads, being a newbie to programming in C# and converted a major function in my application to execute in a background thread (to stop the display from freezing up if I switched to another application).

The threading worked perfectly and it even terminates itself after completion. The only complication I have is that I cannot update the various text boxes on my form because they are part of other system threads.

My application thread is not started until after a menu click event. First, I perform a file dialog function to select the file I want to decode. Once the file dialog box is closed and the file is opened, the thread is initialized and the decoding begins (it takes about 9-10 minutes because it builds a large SQL Server database from the decoded file).

I've had to comment out around 5 lines of code which updated my form boxes (they are within the threaded portion of my program). What I'd like to do is to detect sometime after my thread has started whether it has shut itself down, then I could initialize the form textboxes and listbox afterwards. I don't want to cause the application to freeze the display again. How can I do this Can I do it within my menu click event at a point after the thread has been started

I did experiment to see if any code would execute within my thread after the Thread.Abort() was called, but it pretty much shuts the thread down immediately with no way of executing any lines of code that followed it (this was what I figured would happen, but it was a test to build on my newly acquired knowledge)!

Thanks,

Sean.



Answer this question

Detection of thread executing

  • panagorafredrik

    I am having difficulty with looking up this Invoke problem. All the examples I find are mainly to do with TextBoxes.
    My requirements are to update my ListBox. The 5 lines of code within my thread area I mentioned earlier are as follows:

    this.NRT_StationNamesListBox.Items.Clear();
    for (i = 0; i < StationCodes.NumberOfStations; i++)
    {
    this.NRT_StationNamesListBox.Items.Add(StationCodes.sNRT_StationNames[ i ] );
    }

    What extra things do I need to do to turn this code into an invoke method (lines 1 and 4 need to be addressed)


  • Martin McNally

    You are welcome, please lett us know if you have any problems with this solutions!


  • Stephen Hauck

    Thank you PJ, this worked very well!

    Kind Regards,

    Sean


  • VBnerd

    Create a special method for this:


    protected void UpdateUI()
    {
    if( InvokeRequired )
    {
    MethodInvoker method = new MethodInvoker( UpdateUI );
    Invoke( method );
    return;
    }

    this.NRT_StationNamesListBox.Items.Clear();
    for (i = 0; i < StationCodes.NumberOfStations; i++)
    {
    this.NRT_StationNamesListBox.Items.Add(StationCodes.sNRT_StationNames[ i ] );
    }
    }



    Call this method from your thread when needed.


  • NicksterFL

    Sean Connolly wrote:
    The down-side to this is that I cannot update the boxes within the thread. I got execution errors because the updating and refreshing of system textboxes etc are handled by system based threads, so I commented out the 5 lines of code which did change the textboxes etc...


    Yes you can, just check if invoke is required and if it is, use the Invoke method of the control to execute the method.

    Sean Connolly wrote:
    My thread now executes the same program loop that it did before, just running in the background, but when the end of the program loop is reached, the thread does:

    myThread.Abort();


    Why you use Abort Normally you should never use the Abort method in such situations. I don't know you full situation, but when you can do it a other way, do it a other way.

    Sean Connolly wrote:
    So, in simple terms, the executing thread shuts itself down when it gets to the end (as it is no longer required). As I couldn't update the items on my form during the thread execution, I need to do it once the thread has terminated. That's why I need to be able to wait for that to happen! Because the last excuted item in my menu click event is the call to start the thread, I was hoping that it would be a logical place where I could wait until the thread stopped executing so I could perform the final update of my form.


    I geus it is better to update your UI while running the thread, but when you do it on the end you can use Events. For the UI updating process you should use Events also to lett your UI know that it can be updated.


  • Fabrice MARGUERIE MVP

    I don't really understand you post, you want to determ if a thread is terminated and you want to update you UI afterwards


  • Morten_73

    The way the program used to execute was to sit in a loop until the end, then when the database was built, the TextBoxes and Listbox were updated. Now that my file decoding and SQL database build is running in a background thread, my display no longer freezes if I jump to another application.

    The down-side to this is that I cannot update the boxes within the thread. I got execution errors because the updating and refreshing of system textboxes etc are handled by system based threads, so I commented out the 5 lines of code which did change the textboxes etc...

    My thread now executes the same program loop that it did before, just running in the background, but when the end of the program loop is reached, the thread does:

    myThread.Abort();

    So, in simple terms, the executing thread shuts itself down when it gets to the end (as it is no longer required). As I couldn't update the items on my form during the thread execution, I need to do it once the thread has terminated. That's why I need to be able to wait for that to happen! Because the last excuted item in my menu click event is the call to start the thread, I was hoping that it would be a logical place where I could wait until the thread stopped executing so I could perform the final update of my form.


  • Detection of thread executing