-
Aug 16th, 2013, 08:28 AM
#1
Thread Starter
Frenzied Member
[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()
-
Aug 16th, 2013, 09:28 AM
#2
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
-
Aug 16th, 2013, 11:16 AM
#3
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...
-
Aug 16th, 2013, 12:19 PM
#4
Thread Starter
Frenzied Member
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.
-
Aug 16th, 2013, 12:30 PM
#5
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
-
Aug 16th, 2013, 12:32 PM
#6
Thread Starter
Frenzied Member
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.
-
Aug 16th, 2013, 12:41 PM
#7
Thread Starter
Frenzied Member
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:
private void threadFunc(){ while(running) { // other stuff... if(condition) { running = false; conditionmet = true; continue; } } if(conditionmet) { conditionmet = false; // execute function that raises event on main thread. } }
So, after the event is raised, the thread is certainly going to finish (it's outside the loop at that point).
Sorry.
-
Aug 16th, 2013, 01:51 PM
#8
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
-
Aug 16th, 2013, 02:44 PM
#9
Re: [RESOLVED] Question about Threads.
Originally Posted by Shaggy Hiker
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!?
-
Aug 16th, 2013, 07:51 PM
#10
Thread Starter
Frenzied Member
Re: [RESOLVED] Question about Threads.
Originally Posted by chris128
PS - C# on a VB forum? What is this blasphemy!?
> : )
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|