none
COM and Multithrading RRS feed

All replies

  • How to know that the main thread is busy or free in c#?

    You have a global blnBusy flag, and you set it to true when method on the thread is doing something, and set it to false when method is done. You check the flag for busy.
    Friday, October 27, 2017 12:12 PM
  •  The scenario is as follows.

    Currently I am in worker thread and i want to invoke some method on main thread using dispatcher.

    Before using this method i want to ensure that the main thread is not busy. 

    Is there any way to make sure this.

    sample Code 

    //this function called from worker thread

    public void fuctionFromKernel()

    {

    if(MainThread is not busy)

     m_dispatcher.InvokeAsync(() => { callfunctionInMainTread; });

    else

    through exception

    }

    Friday, October 27, 2017 12:31 PM
  •  The scenario is as follows.

    Currently I am in worker thread and i want to invoke some method on main thread using dispatcher.

    Before using this method i want to ensure that the main thread is not busy. 

    Is there any way to make sure this.

    sample Code 

    //this function called from worker thread

    public void fuctionFromKernel()

    {

    if(MainThread is not busy)

     m_dispatcher.InvokeAsync(() => { callfunctionInMainTread; });

    else

    through exception

    }

    You're the one executing methods on the thread, and you'll know if the thread is busy by the global flag that has been explained.
    Friday, October 27, 2017 12:52 PM
  • You're the one executing methods on the thread, and you'll know if the thread is busy by the global flag that has been explained.

    I would argue that using a global flag is of no help. The worker thread can test the global flag for "busy", and if false call back to the main thread, and in the meantime the main thread becomes busy again. It is a race condition.

    To the OP: you shouldn't care whether the main thread is busy or not. If the main thread is properly coded, it shouldn't be busy for very long. 



    • Edited by Brian Muth Friday, October 27, 2017 2:32 PM
    Friday, October 27, 2017 2:31 PM
  • Thanks for your quick reply.

    I am using following code .

    I have kept Main thread pointer at a my class instance.Then using that instance i am checking whether that thread is busy or not?

    Please tell me is it right way to do this?

                ThreadState state = m_instance.m_thread.ThreadState;
                if (state == ThreadState.Running)
                {

                         //main thread is busy

                               //Through an exception             

                }
                else
                {
                    m_dispatcher.InvokeAsync(() => {callfunctionInMainTread(); });
                }

    Monday, October 30, 2017 5:45 AM

  • Hi  Ganesh_Chavan,

    >>I have kept Main thread pointer at a my class instance.Then using that instance i am checking whether that thread is busy or not?

    As far as I know, you shouldn't care whether the main thread is busy or not. As the Invoke call will not return until the method processes, which won't happen until the thread is not busy.

    You can just call invoke and not worry about whether the main thread is busy.

    >>Please tell me is it right way to do this?

    Your following code will check the thread is running, not busy. If your "busy" means to detect whether it is running, it can be used as such.

    You can refer the following article about ThreadState.

    ThreadState Enumeration:
    https://msdn.microsoft.com/en-us/library/system.threading.threadstate(v=vs.110).aspx

    So, When you open a thread, it will run by all forces, and there is nothing busy, idle state. The  major states that are available, and are not available.

    Our system resource manager will have an intuitive display of busy state, For example, the use of system resources has exceeded 80%. But this "busy" concept is not applicable in a single thread.

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, October 30, 2017 7:04 AM
    Moderator
  • Thanks for your reply.
    Wednesday, November 22, 2017 8:12 AM