Get Thread Result (IAsyncResult)
-
public bool resultado() { ... ... ... return true; }
let's say that i'm created a Thread and i run the resultado method. How i can capture the result? msdn didn't help me :doh: (and, of course, stop the Thread) -
public bool resultado() { ... ... ... return true; }
let's say that i'm created a Thread and i run the resultado method. How i can capture the result? msdn didn't help me :doh: (and, of course, stop the Thread)check this out http://authors.aspalliance.com/aspxtreme/sys/IAsyncResultClass.aspx[^]
-
public bool resultado() { ... ... ... return true; }
let's say that i'm created a Thread and i run the resultado method. How i can capture the result? msdn didn't help me :doh: (and, of course, stop the Thread)A thread terminates when it has no more code to execute (it gets to the end of the method you set as it's start method). You REALLY need to understand how threads interact or you will just spend your entire time debugging things you do not know anything about. Read some tutorials, for example this one: MSDN[^] Generally speaking you communicate with one of the following methods: 1) Communicate with Events (ManualResetEvent, AutoResetEvent). This is typically used to signal things like "data inserted in queue", "terminate" etc. The advantage is that a thread can wait for an event to be raised without consuming CPU resources. 2) Shared data protected by locks. For example a queue one thread is inserting in while another thread reads, or the tread setting a value on a "result object" passed to it. Simple datatypes (int, bool etc) can be marked by the keyword "volatile" instead of using a lock for each access. 3) Invoking from your subtread to the main thread (look at the Dispatcher object, or the old WinForm.Invoke stuff). This allows updating for example a WinForm userinterface where only the GUI thread is allowed to change control values, but is also a pretty sollid way to ensure data is only manipulated on one thread which means you do not need to do locks (as long as you make sure the worker thread is always working on a copy of the data). Instead of setting a terminate event you can allso raise a ThreadInterupt exception, but please notice a thread will NOT interrupt while executing your code - it will ONLY interrupt from a safe state, like waiting for an event... and in that case you can as well have it wait for a terminate event as well. :)
-
A thread terminates when it has no more code to execute (it gets to the end of the method you set as it's start method). You REALLY need to understand how threads interact or you will just spend your entire time debugging things you do not know anything about. Read some tutorials, for example this one: MSDN[^] Generally speaking you communicate with one of the following methods: 1) Communicate with Events (ManualResetEvent, AutoResetEvent). This is typically used to signal things like "data inserted in queue", "terminate" etc. The advantage is that a thread can wait for an event to be raised without consuming CPU resources. 2) Shared data protected by locks. For example a queue one thread is inserting in while another thread reads, or the tread setting a value on a "result object" passed to it. Simple datatypes (int, bool etc) can be marked by the keyword "volatile" instead of using a lock for each access. 3) Invoking from your subtread to the main thread (look at the Dispatcher object, or the old WinForm.Invoke stuff). This allows updating for example a WinForm userinterface where only the GUI thread is allowed to change control values, but is also a pretty sollid way to ensure data is only manipulated on one thread which means you do not need to do locks (as long as you make sure the worker thread is always working on a copy of the data). Instead of setting a terminate event you can allso raise a ThreadInterupt exception, but please notice a thread will NOT interrupt while executing your code - it will ONLY interrupt from a safe state, like waiting for an event... and in that case you can as well have it wait for a terminate event as well. :)
Thanks You i'm working with basic Threads, like
Thread obj = new Thread(method);
but now i need to expand my brain =) reading msdn =)