Secondary Thread start Method in Main Thread

  • Thread starter Thread starter Dale Lundgren
  • Start date Start date
D

Dale Lundgren

I have a c# class library that launches a Win Form in a secondary thread.
From the Form (now running in the secondary thread) I need to be able to
start a method that is defined in the class and have it run in the main
thread.

I have read many examples and tried numerous approaches based on delegates
all of which will launch the method, but never in the main thread, always in
the secondary thread. Is what I'm trying to accomplish feasible?

Here's a quick example:

Class code:

namespace ThreadInvoke
{
public delegate void MyEventHandler(string txt);

public class Class1
{
Form2 myNewForm;

public Class1()
{
// Create instance of form to launch in new thread
myNewForm = new Form2(new MyEventHandler(LocalMethod));
}

public void LaunchThread()
{
// Create new thread, name it and start it
Thread myNewThread = new Thread(new ThreadStart(PopForm));
myNewThread.Name = "Secondary";
myNewThread.Start();
}

private void PopForm()
{
myNewForm.ShowDialog();
}

private void LocalMethod(string txt)
{
MessageBox.Show("Current Thread = " + Thread.CurrentThread.Name
+ "\nCalling Thread = " + txt);
}
}
}

Form Code (partial):

MyEventHandler localHandler;

public Form2(MyEventHandler EventRef)
{
InitializeComponent();
localHandler = EventRef;
}

private void button1_Click(object sender, System.EventArgs e)
{
localHandler(Thread.CurrentThread.Name);
}

Any help will be greatly appreciated!

Dale Lundgren
 
Dale Lundgren said:
I have a c# class library that launches a Win Form in a secondary thread.
From the Form (now running in the secondary thread) I need to be able to
start a method that is defined in the class and have it run in the main
thread.

Have a look at BeginInvoke, it should do what you want.

There's also the BackgroundWorker class in .net2 (and there's c# source to
use in .net1.1 floating around the web somewhere) which has two events, one
fires in a secondary thread (DoWork), and the second fires when that's
finished, back in the main thread (RunWorkerCompleted).
 
Hi Danny-

Thank you for the quick response...

I have used "BeginInvoke" 2 different ways. First, calling it from the
delegate which still launches in the secondary thread. Secondly, calling
BeginInvoke on a Control in a Windows Form App worked great, but since my
main thread is not UI, I cannot seem to duplicate this behavior.

Dale
 
Dale Lundgren said:
Hi Danny-

Thank you for the quick response...

I have used "BeginInvoke" 2 different ways. First, calling it from the
delegate which still launches in the secondary thread. Secondly, calling
BeginInvoke on a Control in a Windows Form App worked great, but since my
main thread is not UI, I cannot seem to duplicate this behavior.

Sorry, I didn't read your post properly!

I've not really played with threads outside of a windows forms app (where
BeingInvoke on a control runs in the thread that created the control, as you
already know). :(
 
Back
Top