Results 1 to 10 of 10

Thread: [RESOLVED] Question about Threads.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Resolved [RESOLVED] Question about Threads.

    Is there a way to asynchronously call a function on the main thread from another thread?
    Basically a 'fire and forget' so that the other thread will not hang up on the .Join()?

    The basic flow is:

    Start main thread.
    Start other thread that monitors a queue of requests.
    If request matches certain criteria, log off.
    Log off raises event on main thread that requests a join from other thread.
    application hangs.

    I know why it's hanging just looking for a solution.

    Thanks,

    Justin

    ** Think I found it : (. BeginInvoke() and EndInvoke()
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Question about Threads.

    You might also want to search the forum on SynchronizationContext. If you Post to the UI SynchronizationContext, the method will run on the UI thread.
    My usual boring signature: Nothing

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Question about Threads.

    If you don't want your main thread to wait for the other thread then why call the Join method? That's like saying I don't want to delete this file but every time I call IO.File.Delete it deletes a file - obviously the solution would be to just not call IO.File.Delete and in your scenario the solution is to just not call Join, unless I'm completely misunderstanding what you want to do...
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Question about Threads.

    I realize that. But I also don't want to just say 'to hell with it' and potentially leave the thread running. Join() is to wait for the thread to end (make sure it terminated successfully). The way the set up was, it would never terminate because the event was raised from the thread that I joined... Anyways, I'm either just going to not join it or do an AsyncCallBack.

    Thanks,

    Justin

    ** Even with the async call back, the event will still be invoked on a ThreadPool thread, but there's no looping in the callback so it will terminate for sure. I just might be over thinking it, I don't know. I mean, is it bad to not join your threads?
    Last edited by MonkOFox; Aug 16th, 2013 at 12:23 PM.
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Question about Threads.

    It's not necessary if you know that the thread is doing nothing. You can always just abort if, but that's a blunt instrument approach that is generally not a good idea, since you would have no way of knowing exactly where it stopped. There are times when that's ok, but not if you are dealing with anything that should be properly disposed.

    Other than that, I'm not sure that there would be a problem. Take a look at the IsBackgroundThread property (I may have the spelling off a bit). This often does something different from what people think it does. What it actually does is changes whether the thread will continue to completion even if the UI thread terminates, or whether it will be shut down when the UI thread shuts down. If you don't care which happens, then it is likely that you don't need to JOIN under any circumstance, since you don't care what happens to the thread.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Question about Threads.

    I'll just let it go without joining then. I'll just keep a log for that individual process to see if any errors creep up.

    Thanks for the info.
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Question about Threads.

    Yeah, umm I was definitely being silly. Here is the basic structure of the function that runs in another thread:

    C# Code:
    1. private void threadFunc(){
    2.  
    3. while(running)
    4. {
    5.      // other stuff...
    6.      if(condition)
    7.      {
    8.           running = false;
    9.           conditionmet = true;
    10.           continue;
    11.      }
    12. }
    13.  
    14. if(conditionmet)
    15. {
    16.      conditionmet = false;
    17.      // execute function that raises event on main thread.
    18. }
    19.  
    20. }

    So, after the event is raised, the thread is certainly going to finish (it's outside the loop at that point).

    Sorry.
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: [RESOLVED] Question about Threads.

    Sometimes, writing out the problem presents the solution. There are many threads I have almost, but not quite, started. As I wrote it, I saw the problem.
    My usual boring signature: Nothing

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Question about Threads.

    Quote Originally Posted by Shaggy Hiker View Post
    Sometimes, writing out the problem presents the solution. There are many threads I have almost, but not quite, started. As I wrote it, I saw the problem.
    haha I'm glad its not just me that does that! Although in this case I'd like to think my reply was what pointed the OP in the correct direction rather than him just writing the question out, but maybe that's just me wanting to feel like I've helped

    PS - C# on a VB forum? What is this blasphemy!?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: [RESOLVED] Question about Threads.

    Quote Originally Posted by chris128 View Post
    PS - C# on a VB forum? What is this blasphemy!?
    > : )
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width