Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-07-03
4
Medium Priority
?
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

661 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