[C#] Difference with Invoke and Threading

Status
Not open for further replies.

litewarez

Active Member
1,367
2008
1
0
Im just wondering, is there any benefits when it comes to Invoke or Threading.

and what's the difference:

PHP:
namespace Test
{
    publuc class MyBox : Form()
    {
         public delgate SomeDelegate();

         public void MyBox()
         {
             InitializeComponant();
             
             SomeLibrary.OnFinished += new SomeDelegate(Method);
         }

        public void Method()
       {
           if(InvokeRequired)
           {
               new SomeDelegate(Method);
               return;
           }
            //Access Here :/
       }
    }
}

So my point is, if i start using the Invoke throughout my applications, will there be some down side ?
 
5 comments
The Control.Invoke() method and Thread object are completely different so they can't have benefits over each other.

And what's the difference between what?

Why would there be some downside? Invoking is something that is required for doing cross thread calls.
 
I mean, When you use Control.Invoke, Is that Invoking a method on another Thread, or is that creating a new thread and then invoking the method.

Also, By doing new Thread(new Action(() => SomeClass,new object[] {this});.. Does that create a new thread and runs the object within that thread, and then to use that class i would have to use a Invoke?

Sorry ive just learnt that Invoking stops plenty of errors :), and wanted to understand it a little more.
 
You need to understand threading. If a thread is trying to access an object that it did not create, you need to invoke that object.

Example:

Main Thread: The initial thread used to create the program.
- Draws a textBox called TextBox1
- Draws a button called button1

Thread2 - self-created Thread.
- Tries to access TextBox1.
- Is returned an error "cannot access object thread did not create".
- Solution: Invoke the object.

This is a common example of useage for invoking objects whilst using threading.
 
Status
Not open for further replies.
Back
Top