Hello

I created the following project to show you guys how I plan to do things. But my main project will be bigger and will have multiple classes. I'm just trying to get this to work properly so I know I'm using good practice when coding.

ok, lets begin :), so my form has a button called "button1" and a text box called "textBox1" and I have a class called "Class1" which has a method "testtest()", I just put Thread.Sleep in the testtest method so I can find out its running on another thread.

Here is my form code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    delegate void Class1Deligate();

    private void button1_Click(object sender, EventArgs e)
    {
      Class1 c = new Class1();
      Class1Deligate testMethod = new Class1Deligate(c.testtest);
      testMethod.BeginInvoke(endTestMethod, testMethod);
    }

    void endTestMethod(IAsyncResult ar)
    {

    }
  }
}

and here is my Class one code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Test
{
  class Class1
  {
    public void testtest()
    {
      Thread.Sleep(8888);
      MessageBox.Show("Done");
    }
  }
}

Am I creating a new thread correctly? And can someone please tell me how to update textbox1 from the testtest method in class1 while its running? I made a earlier post and I was told to use Dispatcher, but for some reason it seems the Dispatcher class is not available for me.

Best Regards!

Nope, you don't have a Thread.Start anywhere in your code. Take a look at this free ebook.

Nope, you don't have a Thread.Start anywhere in your code. Take a look at this free ebook.

Thanks for your reply, but that e-book does not contain any examples with a GUI. What confuses me is that in most examples with GUI deligate.BeginInvoke() is used and I'm not sure why, I have been researching this for a week and I'm still confused about it. Take a look at this link http://msdn.microsoft.com/en-us/library/ms951089.aspx

You use begin invoke when the process is running on a different thread. Since you haven't started another thread, using BeginInvoke doesn't do anything for you. Also, it does talk about GUI threads, right here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.