Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. running a function in a thread

running a function in a thread

Scheduled Pinned Locked Moved C#
mobiledesignquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mridang Agarwalla
    wrote on last edited by
    #1

    i havea function in my app called CheckStatus. This take no prameters and returna a boolean value as the result. I am using it in a if..else statement like this: if (CheckStatus() == true) //Do somethins here else //Do something else here Now what happening is that CheckStatus function takes some time to process and due to this my UI becomes non responsive. How can i acheive the above functionality by putting it in a separate thread. I'm pretty new to using Threading so it'd be great if someone could explain in a bit of detail. Thaks in advance!

    A 1 Reply Last reply
    0
    • M Mridang Agarwalla

      i havea function in my app called CheckStatus. This take no prameters and returna a boolean value as the result. I am using it in a if..else statement like this: if (CheckStatus() == true) //Do somethins here else //Do something else here Now what happening is that CheckStatus function takes some time to process and due to this my UI becomes non responsive. How can i acheive the above functionality by putting it in a separate thread. I'm pretty new to using Threading so it'd be great if someone could explain in a bit of detail. Thaks in advance!

      A Offline
      A Offline
      Anh_Tuan
      wrote on last edited by
      #2

      Create a new thread to process time-consuming tasks. Here is a simple example using System.Threading; ... ThreadStart ts = new ThreadStart(DoWork); Thread t = new Thread(ts); t.Start(); ... void DoWork() { // do my work here } Check out more at at Threading articles on here or MSDN Lib.[^] Jup

      A 1 Reply Last reply
      0
      • A Anh_Tuan

        Create a new thread to process time-consuming tasks. Here is a simple example using System.Threading; ... ThreadStart ts = new ThreadStart(DoWork); Thread t = new Thread(ts); t.Start(); ... void DoWork() { // do my work here } Check out more at at Threading articles on here or MSDN Lib.[^] Jup

        A Offline
        A Offline
        Adadurov
        wrote on last edited by
        #3

        I use to do it like this: class SomeClass { public Routine1() { // Show "Wait" cursor (hour-glass) System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; // reset completion flag workDone = false; ThreadStart ts = new ThreadStart(DoWorkWithStatusChecking); Thread t = new Thread(ts); t.Start(); // in this loop we wait for completion and // processing GUI messages. while( ! t.IsDone this.workDone ) { // If you need to prevent user's interaction with GUI // (clicking around, pressing buttons, etc.) // you can pass message filter to Application.DoEvents() System.Windows.Forms.Application.DoEvents(); // following line is here to prevent 100% CPU load // but some people think it's not necessary // i just dont have time to check it, 'cause it works just fine Sleep(0); } } bool workDone = false; void DoWorkWithStatusChecking() { // This is what you want to do after status-checking. if( this.Status ) { } else { } // ... done, set this.workDone = true; } bool CheckStatus() { // ... real status checking here return ( true | false ); } } // end class SomeClass Note: routine1 is not reenterable and must be guarded by some sort of guard condition in the beginning! got the idea? Alexei.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups