Solved

Main thread calling event from child thread without using Control.Invoke

Posted on 2006-07-03
4
332 Views
Last Modified: 2012-05-07
Hi Experts,

I build an object with a method and an event.
In the method, I start a thread(child thread) and after a long process, the event is raised (from the child thread).

When I use this object in my form, I get an error which tells me to use Textbox.Invoke (which I understand and I know how to solve the error from my form). No problem.

The problem is I want the event to be raised in main thread, so no Textbox.Invoke error will be thrown in my form.

Thanks in advance for your help.
0
Comment
Question by:noulouk
  • 2
  • 2
4 Comments
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 17029631
Hi noulouk,

I can't personally see anyway to force an event to fire on the main UI thread, the only thing you can really do in my opinion is to use the InvokeRequired check before doing anything on your UI.

Sorry!

Wint.
0
 
LVL 9

Author Comment

by:noulouk
ID: 17029659
Thanks Wint,

Main thread means the main thread of my object. No matter if this is main UI thread or a child of the main UI thread that instanciate my object.
Hope you see what I need and can help me more.

0
 
LVL 9

Accepted Solution

by:
WinterMuteUK earned 500 total points
ID: 17029945
The only way I can think of to get it back on the main thread is to actually raise the event from the main thread itself:

    internal class TestClass
    {
        public event EventHandler MyEvent;

        public void MyMethod()
        {
            Thread.CurrentThread.Name = "Main";
            Thread child = new Thread(new ThreadStart(ChildMethod));
            child.Start();

            child.Join(); //Will stop this running until child is finished.
            RaiseEvent( this, null ); //On main thread.
        }

        void RaiseEvent( object sender, EventArgs e )
        {
            Console.WriteLine("On thread: " + Thread.CurrentThread.Name);
            if ( MyEvent != null )
                MyEvent( sender, e );
        }

        private void ChildMethod()
        {
            Thread.CurrentThread.Name = "Child";
            Thread.Sleep(2000);
            RaiseEvent(this, null); //on child thread.
        }
    }

but it's not very nice, because it relies on the child only raising the event when it's finished - and obviously looses any point really of using a child thread at all!

Wint.
0
 
LVL 9

Author Comment

by:noulouk
ID: 17032302
This works, but this is very ugly as you tell.
Thanks anyway.

Regards.
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question