Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# and multithreading

Options
  • 05-09-2010 8:19pm
    #1
    Closed Accounts Posts: 22,479 ✭✭✭✭


    I'm currently working on a project mainly out of plain interest in C# a language I'm really only starting getting to grips with (used to Java and PHP mainly). The program uses a thread, which is a link to another method in the program and with thread.Start() to begin the new thread.

    Code example:
    Thread thread = new Thread(methodHere);
    			thread.Start();
    

    Does the thread automatically terminate when the method is finished? Or does it need to be stopped manually by another function?


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Jakkass wrote: »
    I'm currently working on a project mainly out of plain interest in C# a language I'm really only starting getting to grips with (used to Java and PHP mainly). The program uses a thread, which is a link to another method in the program and with thread.Start() to begin the new thread.

    Code example:
    Thread thread = new Thread(methodHere);
    			thread.Start();
    

    Does the thread automatically terminate when the method is finished? Or does it need to be stopped manually by another function?
    Yes, as soon as it exits the "function", the thread is finished (gracefully).

    Most threads enter an event loop state or get locked waiting for data to purchase.

    What does the function do.


  • Closed Accounts Posts: 22,479 ✭✭✭✭philologos


    It's for an IRC bot I'm writing. When it receives the nicklist from the server, it dispatches another thread to write the list of names to the MYSQL server so as not to hang up the main thread reading the data from the server.

    Am I right in saying that if there is no loop in the method I am calling, and if it finishes writing to the MYSQL server the thread is finished?


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Jakkass wrote: »
    It's for an IRC bot I'm writing. When it receives the nicklist from the server, it dispatches another thread to write the list of names to the MYSQL server so as not to hang up the main thread reading the data from the server.
    Yeah well, in that case it should terminate itself.


  • Registered Users Posts: 515 ✭✭✭NeverSayDie


    Jakkass wrote: »
    I'm currently working on a project mainly out of plain interest in C# a language I'm really only starting getting to grips with (used to Java and PHP mainly). The program uses a thread, which is a link to another method in the program and with thread.Start() to begin the new thread.

    Code example:
    Thread thread = new Thread(methodHere);
    			thread.Start();
    

    Does the thread automatically terminate when the method is finished? Or does it need to be stopped manually by another function?

    It runs until the thread procedure is finished, you don't need to cut it off at some point yourself.


  • Registered Users Posts: 139 ✭✭seithon


    I'm learning C# myself.. I think their are also methods you can use to pause or terminate a thread manually if you need to :)


  • Advertisement
  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    seithon wrote: »
    I'm learning C# myself.. I think their are also methods you can use to pause or terminate a thread manually if you need to :)
    It's bad practice to call the Abort methods on a thread. If you want to abort a thread that has a loop you should have a flag that's set so the loop knows it should leave. This flag is set by your other thread that wants to terminate it.


Advertisement