Solved

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

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

734 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