473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Secondary Thread start Method in Main Thread

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
Nov 17 '05 #1
3 3749
"Dale Lundgren" <dl*******@rand.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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).
Nov 17 '05 #2
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

"Danny Tuppeny" <gr****@dannytuppeny.commmmmm> wrote in message
news:43**********************@ptn-nntp-reader01.plus.net...
"Dale Lundgren" <dl*******@rand.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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).

Nov 17 '05 #3
"Dale Lundgren" <dl*******@rand.com> wrote in message
news:eK**************@TK2MSFTNGP14.phx.gbl...
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). :(
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Serge | last post by:
Hi, I am having a thread hang problem in my c# code. The example on the website: http://csharp.web1000.com/ is a simplified version of my problem. You will see in the form that a...
0
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs...
7
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs...
2
by: BG | last post by:
We're having trouble writing the code to update a UI control (label.Text) from a secondary thread. We're using C# with Windows Forms. We have a main form named MainForm, a splash screen form...
6
by: Daniel | last post by:
i have an array that i want all threads to be able to READ from concurrently, however, at times i want to UPDATE the array. at which point i want all threads that use it to block when they try to...
7
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have...
2
by: Don | last post by:
How to stop a process which is running in a separate thread!!! I've got a class which performs some lengthy process in a background (separate) thread. And this lengthy process raises events...
9
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
8
by: Daniele Piccinini | last post by:
Hi all, In my vc++ 2005 dialog based application i need to use a comunication activex component in a secondary thread: CFINSAxEFS* pNewAx = new CFINSAxEFS(); if ( !pNewAx->Create( NULL,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.