Creating and Using Windows.Forms inside Threads
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Creating and Using Windows.Forms inside Threads

  1. #1
    Join Date
    Apr 2009
    Posts
    6

    Creating and Using Windows.Forms inside Threads

    Hi,

    I am just wondering something related to safety:

    Is it safe to create Windows forms from separated Threads (System.Threading) ?

    The Thread is created from a UserControl (from a click button for example).

    The point is that when the thread is started I need to show some "custom forms" (Windows.Forms) that are created inside the thread in order to promp to the user the -OK and -CANCEL options in order to take a desition from there.

    The thread is a single thread apartment (STA)

    Example:

    Code:
    namespace ThreadTestandForms
    {
        public partial class FormGUI : UserControl
        {
    
            private void button2_Click(object sender, EventArgs e)
            {
                 //WANT TO KEEP GUI RESPONSIVE.
                 //WANT TO KEEP GUI RESPONSIVE.
    
                Thread ThreadSendParts;
                ThreadSendParts = new Thread(new ThreadStart(RunsOnThread));
                ThreadSendParts.SetApartmentState(ApartmentState.STA);
    
                //I want to use a good priority.
                ThreadSendParts.Priority = ThreadPriority.Highest;
                
                ThreadSendParts.Start();
    
                FrmSentPrtPgres.ShowDialog();  //I need to show a modal dialog after running the thread.
            }
    
    
            private void RunsOnThread()
            {
                for (int i = 0; i <= 100; i += 10) //EMULATE VERY HEAVY PROCESS
                {
                    Thread.Sleep(200); //EMULATE VERY HEAVY PROCESS
    
    
                    //This method uses BeginInvoke and InvokeRequired
                    ShowProgress("My Progress Status", i);
    
                    //Example of doing some custom form with some custom conditions.
                    FrmMyMessage MyForm = new FrmMyMessage("Do you want do this or that?");
    
                    //show message stop exectuting until user takes a decision.
                    DialogResult DLRes = MyForm.ShowDialog();
    
                    if (DLRes == DialogResult.OK)
                    {
                        //do something this
                    }
                    else
                    {
                        //do something that
                    }
                }
    
                //Execute some extra things when finishing the thread.
                //This method uses BeginInvoke and InvokeRequired
                ThreadCompleted();
            }
        }  
    }
    Regards,
    Last edited by ennrike; April 13th, 2009 at 06:30 PM.

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: Creating and Using Windows.Forms inside Threads

    Hi !
    At first welcome to forum.
    As a second sorry to say, but please reread forum rules as it is not ok to post code without using codetags. Code without codetags losts format and nobody is intersted to read cde sausage.
    In the editor you will find a button 'code' which sets the tags for you and you only need to put the code inbetween. Such easy. If you want to do it by hand look into the bottom of my post you'll see how it is done.

    As a third your theme. Why not asking your questions in the mainthread and starting your thread afterwards when the decisions already are done?

    And whats the problem you need to do threading ? Please describe so we can suggest a solution
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  3. #3
    Join Date
    Apr 2009
    Posts
    6

    Re: Creating and Using Windows.Forms inside Threads

    Thanks for your reply, as you could notice I am new in the forum, I use a lot the codeguru, at the beginning was very difficult for me to find the "Post New Message/Thread" I think there should be one button in the main GUI of "CodeGuru Forums" and choose from there the area of interest. How can I ask my question in the Mainthread?

  4. #4
    Join Date
    Apr 2009
    Posts
    6

    Re: Creating and Using Windows.Forms inside Threads

    Thanks for the advices, I have reformatted my question.

    I need to use the thread because it is a heavy operation in process and I want to keep responsive UI.

    How can I post in main thread?

    Thanks,
    ennrike

  5. #5
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: Creating and Using Windows.Forms inside Threads

    Quote Originally Posted by ennrike View Post
    Thanks for the advices, I have reformatted my question.

    I need to use the thread because it is a heavy operation in process and I want to keep responsive UI.

    How can I post in main thread?

    Thanks,
    ennrike
    There is nothing such as a main thread. Your post is now corrected and in the correct place. As much as I'm looking to your button2_Click code the
    Code:
    FrmSentPrtPgres.ShowDialog();
    is still in the main thread. But I cannot see this form to be initialized at any place. So I ask you where have you created an nstance of that form you want to show. We are not doing VB6.0 here where you had the Fprm pattern itself as the default Form
    you need to do
    Code:
    FrmSentPrtPgres myForm = new FrmSentPrtPgres ();
    if (myForm.ShowDialog() == DialogResult.OK ){
        // do whatever is needed
    }
    The fact that you started a thread just before calling this dialog doesn't influence your DialogBox here. In the moment I cannot see other problems in the posted code. I personally never have tried such a way of using threads.
    I personally would try to execute the other thread up to the point where I need to request there ending this process. Showing my request in the GUI thread and starting a new process to do the rest of the work. The other way would be to use a delegate and to invoke the request in the main thread then coming back to the requesting thread and going on depending on the result of the request. But in my eyes simple ending the thread at the time the request is needed and starting a new one depending on the results of the dialog will be easier to be done.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  6. #6
    Join Date
    Apr 2009
    Posts
    6

    Re: Creating and Using Windows.Forms inside Threads

    Thanks for the advices Jonny,

    This code is just an illustration of my question, in the real life the line: "Thread.Sleep(200); //EMULATE VERY HEAVY PROCESS" has a method witch executes very slow operations and need to promp forms during this process to ask some questions.

    I guess instead of using "Main thread" I should have used "UI Thread"

    I am also using the method: "ShowProgress" to show another form with a progress bar showing the whole progress, in this case I am using the "BeginInvoke and InvokeRequired" as you pointed out.

    Thank you very much for your advices.
    Last edited by ennrike; April 14th, 2009 at 10:19 AM.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center