Solved

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

Posted on 2006-07-03
4
328 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Unit testing, moq, best practices 7 28
Refresh Datagridview from another form 1 28
Different Delete Messages 7 19
XML & .net 5 21
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now