none
Delegates and Threading RRS feed

  • Question

  • Hi, if i use delegates will i be taking care of threading as well? will my methods in the event.invoke() run on a separate thread?

    Best Regards, Lalith

    Sunday, December 22, 2019 8:41 AM

Answers

  • If you are using System.Threading and a  thread is spawned and pointing to a delegate, then the delegate and all methods the delegate executes run on the spawned thread, which is separate from any other thread. 

    If you are not using System.Threading, then everything happens on the heartbeat thread the program was started up on.

    • Marked as answer by slalithp Sunday, December 22, 2019 12:17 PM
    Sunday, December 22, 2019 8:58 AM
  • Hello,

    If targeting C# 6 or higher use the null conditional operator e.g.

    SomeEvent?.Invoke(this, args);

    Adjust the arguments according to the arguments for your event e.g.

    OnListBoxClickHandler?.Invoke(this, new InformationArgs(listBox1.Text));

    See also

    https://oz-code.com/blog/net-events-thread-safe-and-reliability/  

    Then with that we can write an extension method

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WinFormsExtensionsLibrary
    {
        public static class ControlExtensions
        {
            public static void InvokeIfRequired(this Control c, Action<Control> action)
            {
                if (c.InvokeRequired)
                {
                    c.Invoke(new Action(() => action(c)));
                }
                else
                {
                    action(c);
                }
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Sunday, December 22, 2019 11:13 AM
    Moderator

All replies

  • If you are using System.Threading and a  thread is spawned and pointing to a delegate, then the delegate and all methods the delegate executes run on the spawned thread, which is separate from any other thread. 

    If you are not using System.Threading, then everything happens on the heartbeat thread the program was started up on.

    • Marked as answer by slalithp Sunday, December 22, 2019 12:17 PM
    Sunday, December 22, 2019 8:58 AM
  • nice, kindly give me an example for how to point a thread to a delegate. 

    Best Regards, slalithp


    • Edited by slalithp Sunday, December 22, 2019 12:18 PM
    Sunday, December 22, 2019 9:28 AM
  • Hello,

    If targeting C# 6 or higher use the null conditional operator e.g.

    SomeEvent?.Invoke(this, args);

    Adjust the arguments according to the arguments for your event e.g.

    OnListBoxClickHandler?.Invoke(this, new InformationArgs(listBox1.Text));

    See also

    https://oz-code.com/blog/net-events-thread-safe-and-reliability/  

    Then with that we can write an extension method

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WinFormsExtensionsLibrary
    {
        public static class ControlExtensions
        {
            public static void InvokeIfRequired(this Control c, Action<Control> action)
            {
                if (c.InvokeRequired)
                {
                    c.Invoke(new Action(() => action(c)));
                }
                else
                {
                    action(c);
                }
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Sunday, December 22, 2019 11:13 AM
    Moderator
  • Thank you so much Guys

    Best Regards, slalithp


    • Edited by slalithp Sunday, December 22, 2019 12:18 PM
    Sunday, December 22, 2019 12:17 PM
  • Thank you so much Guys

    Best Regards, slalithp


    Bing and Google are your friends.

    https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread?view=netframework-4.8

    https://docs.microsoft.com/en-us/dotnet/api/system.threading.timer?view=netframework-4.8

    Sunday, December 22, 2019 2:02 PM