none
Cshtml Thread Start RRS feed

  • Question

  • When I execute this statement: 

    Thread th = new Thread(new ThreadStart(Key_Hunter())); //Crash if activated without Function Call...
            th.Start();
            //message = DateTime.Now.Month.ToString();
    }
    
    @functions{
    //Warning: Key_Hunter is OFF-THREAD!!!!!!!!!!!
    //Kill Thread if times out
        void Key_Hunter()
        {
            //Key combo: C-R-A-Z-Y T-E-A-M
            Thread.Sleep(500);
        }
    }
    it crashes when starting the thread. 

    Friday, January 11, 2013 6:51 AM

Answers

  • It works, but how would I put an argument with the function?

    Hi there,

    Please try this way to pass the parameter:

            private void button3_Click(object sender, EventArgs e)
            {
                Thread th = new Thread(new ParameterizedThreadStart(Key_Hunter)); 
                th.Start("abcd");
            }
    
            void Key_Hunter(object  para)
            {
                MessageBox.Show("Parameter is " + para);
                Thread.Sleep(500);
            }

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 15, 2013 2:35 AM
    Moderator

All replies

  • Hi there,

    Welcome to the MSDN Forum.

    I tried your code in VS2010, .net 4.0 winforms application and with a few changes:

            private void button3_Click(object sender, EventArgs e)
            {
                Thread th = new Thread(new ThreadStart(Key_Hunter)); //Crash if activated without Function Call...
                th.Start();
                //message = DateTime.Now.Month.ToString();
            }
            //@functions{
            //Warning: Key_Hunter is OFF-THREAD!!!!!!!!!!!
            //Kill Thread if times out
            void Key_Hunter()
            {
                //Key combo: C-R-A-Z-Y T-E-A-M
                Thread.Sleep(500);
            }

    It works fine.

    The change is: remove the brackets in the threadStart parameter.

    If this is not your situation, would you like to tell me more about how to reproduce this scenario?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 14, 2013 6:12 AM
    Moderator
  • It works, but how would I put an argument with the function?
    Monday, January 14, 2013 11:23 PM
  • It works, but how would I put an argument with the function?

    Hi there,

    Please try this way to pass the parameter:

            private void button3_Click(object sender, EventArgs e)
            {
                Thread th = new Thread(new ParameterizedThreadStart(Key_Hunter)); 
                th.Start("abcd");
            }
    
            void Key_Hunter(object  para)
            {
                MessageBox.Show("Parameter is " + para);
                Thread.Sleep(500);
            }

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 15, 2013 2:35 AM
    Moderator