locked
IRQL Level

    Question

  • Is it possible to change the IRQL level of my program?

    Wednesday, April 11, 2012 4:47 AM

Answers

All replies

  • Hi,

    What do you call the IRQ level of your program ?  Do you mean the "process priority" ? It's possible but unless you have particular needs this is likely the last thing you should try (i.e. if this is for performance issue, it would be better to fix those).

    I would suggest to just tell what you are trying to do rather than asking about how you expect this to be done.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Wednesday, April 11, 2012 9:06 AM
  • I need to create a system thread in Visual Basic for my business application.

    I thought IRQL was the same thing...

    Wednesday, April 11, 2012 6:35 PM
  • if you want to run a thread, use the framework runtime, as described here:

    http://msdn.microsoft.com/en-us/library/3e8s7xdd%28v=vs.100%29.aspx

    IRQs, are a hardware/driver/kernel mechanism/interface that allows hardware and software to share events. they are still important, but not to the vast majority of programmers. it is essential to native driver and kernel development though.

    Wednesday, April 11, 2012 9:20 PM
  • Yes, that is what I'm doing. I need to make my application act as if it's a driver. (It technically is)

    I need very high priorities. Realtime/Time_Critical are not enough.

    Wednesday, April 11, 2012 9:53 PM
  • Yes, that is what I'm doing. I need to make my application act as if it's a driver. (It technically is)

    I need very high priorities. Realtime/Time_Critical are not enough.

    Then VB is not the rigth choice because it creates application running in user mode.

    Windows Driver Kit:
    http://msdn.microsoft.com/en-us/library/ff557573(VS.85).aspx

    Unfortunatelly the topic "Choosing a Programming Language" has been removed from the SDK. It said: "For maximum portability, drivers should be written in a high-level language, typically C for kernel-mode drivers, and C or C++ for user-mode drivers."


    Armin

    Wednesday, April 11, 2012 10:12 PM
  • So is it not possible to do this, or is there any other way to obtain high-level rights for my program? Anything better than priorities in visual basic?

    Wednesday, April 11, 2012 10:32 PM
  • Priorities for user mode processes and threads are not VB or .Net specific. It is what Windows offers.
    http://msdn.microsoft.com/en-us/library/ms685100(VS.85).aspx

    Maybe we can help you without writing a driver if you tell us what you're trying to do. So...?


    Armin

    Wednesday, April 11, 2012 10:48 PM
  • Thank you for your reply. Just two more questions.

    Does this code allow my program to only run on one CPU? (The 2nd CPU)

    ProcessorAffinity = CType(2,IntPtr)

    And, does anyone know of any windows processes that are run as realtime processes?


    • Edited by jonny_mando Thursday, April 12, 2012 12:54 AM
    Thursday, April 12, 2012 12:53 AM
  • Yes.

    In the task manager, show "[x] processes of all users", display the colum "base priority" and sort by it.


    Armin

    Thursday, April 12, 2012 1:38 AM
  • Thank you.

    One last question. Hardware/Drivers are usually ran as RealTime Priority, Correct? So, just the windows processes will appear in the task manager. There are no realtime windows processes, but there are realtime drivers that are invisible?

    Thursday, April 12, 2012 1:52 AM
  • This is handled specially in kernel mode. I don't know much about this. Maybe this helps: http://msdn.microsoft.com/en-us/library/ff564638(VS.85).aspx

    Armin

    Thursday, April 12, 2012 2:03 AM
  • Wow, that link is perfect. Thanks! And, sorry, but, just ONE more question. I'm calling SetThreadPriority API, but it's not setting the thread priority.

    Private Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As
    IntPtr, ByVal nPriority As Integer) As Integer

    SetThreadPriority(Thread.ManagedThreadId,

    ThreadPriorityLevel.AboveNormal)

    However, when I call the:

    Dim

    Test = CurrentProcess.BasePriority

            MsgBox(Test)

    I still receive "24". 24 is the normal thread priority at Realtime process. I have it at AboveNormal, so it should say something above 24.

    Any ideas?

    Thursday, April 12, 2012 2:17 AM
  • You don't have to call the API to set the priority. Just set the priority property of the Thread object:

    System.Threading.Thread.CurrentThread.Priority = Threading.ThreadPriority.AboveNormal

    You must not pass the managed thread ID to SetThreadPriority as it expects a thread handle. That's something different. In theory, the runtime can change the system thread in which the managed thread is running, that's why there is a managed thread ID that does not change. If you wanted to call SetThreadPriority for the current thread you'd have to call API function GetCurrentThread() before, and before that, you'd have to call Thread.BeginThreadAffinity. Long story short:

    Imports System.Threading
    Imports AZ.Win32
    
    Public Class Main
    
       Shared Sub Main()
    
          Thread.BeginThreadAffinity()
    
          Try
             Dim h = Functions.GetCurrentThread()
             Functions.SetThreadPriority(h, AZ.Win32.ThreadPriority.AboveNormal)
             '...
             Functions.SetThreadPriority(h, AZ.Win32.ThreadPriority.Normal)
          Finally
             Thread.EndThreadAffinity()
          End Try
    
       End Sub
    
    End Class
    

    I don't post the declarations as it's only for demonstration.

    But again, API calls are not required.

    Don't mix up the priority class of the process with the priorty of the thread. By setting the latter you don't change the former. Both values are combined for each thread to the base priority as shown in the table in the link I posted some messages ago. The property CurrentProcess.BasePriority is something different again. Actually I haven't come across it yet. As it's readonly, just stick with the thread's Priority property and the PriortyClass property of the process object.


    Armin

    Thursday, April 12, 2012 2:59 AM
  • Thanks, I understand.

    I'm using Visual Basic 2010, so Imports Az.Win32 doesn't exist. Also, your current SetThreadPriority only goes as high as "highest". There is no TIME_CRITICAL option.

    What should I do?

    Thursday, April 12, 2012 3:47 AM
  • Sure az.win32 doesn't exist as it's in my own library, but as I said it's not required to show declarations.

    "What should I do?"

    Tell us what you're trying to do. You don't need to set anything to time_critical, and if you still think you have to, you have to write a driver.


    Armin

    Thursday, April 12, 2012 4:05 AM
  • Sure az.win32 doesn't exist as it's in my own library, but as I said it's not required to show declarations.

    "What should I do?"

    Tell us what you're trying to do. You don't need to set anything to time_critical, and if you still think you have to, you have to write a driver.


    Armin


    Can you post an example code for changing the priority to time_critical without using your own library? The last request. :)
    Thursday, April 12, 2012 4:20 AM
  • Can you post an example code for changing the priority to time_critical without using your own library? The last request. :)

    Just pick out the right declarations and values. I don't think it's necessary, so I won't post it.

    You may be able to lock a foreign system.


    Armin

    Thursday, April 12, 2012 10:09 AM
  • I have:

    Private Declare Function GetCurrentThread Lib "kernel32" Alias "GetCurrentThread" () As Integer

    Private Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As IntPtr, ByVal nPriority As Integer) As Integer

       Public Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Thread = New System.Threading.Thread(AddressOf Superior)
            Thread.Start()
        End Sub


        Public Sub Superior()
            Thread.BeginThreadAffinity()
    Dim CurrentThread = GetCurrentThread()
            SetThreadPriority(CurrentThread, ThreadPriorityLevel.TimeCritical)

    Dim Test = CurrentProcess.BasePriority
            MsgBox(Test)

    Yet, my base priority still says 24.

    Thursday, April 12, 2012 6:18 PM
  • Hi, you set the priority of a background thread and you read the priority of your process. Never done that but could it be the issue ?

    From what I see you could also perhaps just try http://msdn.microsoft.com/en-us/library/system.diagnostics.process.priorityclass.aspx instead which is a read/write property ?

    Finally as hinted by Armin, it's hard to give an advice without knowing what is the problem you are trying to solve. Sometimes it allows one to suggest an alternate better solution or even to avoid wasting time by finding out that the current worked on solution just won't cut it.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Friday, April 13, 2012 8:22 AM
  • Hi, you set the priority of a background thread and you read the priority of your process. Never done that but could it be the issue ?

    From what I see you could also perhaps just try http://msdn.microsoft.com/en-us/library/system.diagnostics.process.priorityclass.aspx instead which is a read/write property ?

    Finally as hinted by Armin, it's hard to give an advice without knowing what is the problem you are trying to solve. Sometimes it allows one to suggest an alternate better solution or even to avoid wasting time by finding out that the current worked on solution just won't cut it.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    I'm just trying to set my base priority to the maximum (31).

    This should be all I need to do for my program.

    The base priority is the combined priority of my thread and process priority. So, since I called the GetBasePriority function during my main thread (Superior) - after I used SetThreadPriority - it should have given me my desired base priority.

    So, how can I achieve a Base Priority of 31?

    Friday, April 13, 2012 5:22 PM
  • I ran the GetThreadPriority function and it gave me '15'. My process is set to realtime. It seems to me that 'SetThreadPriority' doesn't seem to be working at all. I even tried using it with 'GetCurrentThread'.

    I don't understand.

    Friday, April 13, 2012 6:15 PM
  • Show us the code that doesn't seem to work.

    Don't care about Process.BasePriority. Use Thread.Priority and Process.PriorityClass. If it makes you happy, you can use SetThreadPriority to the set thread priority to time critical which is not available in System.Threading.ThreadPriority.


    Armin

    Friday, April 13, 2012 6:32 PM
  • Show us the code that doesn't seem to work.

    Don't care about Process.BasePriority. Use Thread.Priority and Process.PriorityClass. If it makes you happy, you can use SetThreadPriority to the set thread priority to time critical which is not available in System.Threading.ThreadPriority.


    Armin

    Thank you for this reply. :)

    I used Process.PriorityClass and set my current process to realtime. This works. :)

    As above posted, I used GetCurrentThread, then GetThreadPriority, then SetThreadPrioirty and it gave me a priority of 15. Well, there is no 15 priority for a realtime process, so I don't know what's wrong. :(

    My multithreading code is posted above. I used SetThreadPriority right after I started my thread.

    Friday, April 13, 2012 6:43 PM
  • The declaration of GetCurrentThread is wrong. Return type must be IntPtr. I can not compile it. Always use Option Strict On.

    "Well, there is no 15 priority for a realtime process, so I don't know what's wrong."

    The name is GetThreadPriority and SetThreadPrioirty. The valid range is -15..15, so I don't see the problem.

    A process has a priority class. It must be one of the predefined values (Idle, Normal, etc.) It's value has no relation to the thread priority.

    Both values, the priority class of the process and the priority of the thread, form the base priority of the thread as shown in the linked table.



    Armin

    Friday, April 13, 2012 7:18 PM
  • The declaration of GetCurrentThread is wrong. Return type must be IntPtr. I can not compile it. Always use Option Strict On.

    "Well, there is no 15 priority for a realtime process, so I don't know what's wrong."

    The name is GetThreadPriority and SetThreadPrioirty. The valid range is -15..15, so I don't see the problem.

    A process has a priority class. It must be one of the predefined values (Idle, Normal, etc.) It's value has no relation to the thread priority.

    Both values, the priority class of the process and the priority of the thread, form the base priority of the thread as shown in the linked table.



    Armin

    Thanks, I changed it to IntPtr.

    No, the value range is -15-15 if you have a priority other than realtime class.

    Realtime classes start at 16 thread priority and go up to 31. (31 is what I need):

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms685100(v=vs.85).aspx

    Yes, I know, I set my process priority to realtime.

    Yes, I know, my current base priority is 24. It needs to be 31.

    Friday, April 13, 2012 7:41 PM
  • http://msdn.microsoft.com/en-us/library/windows/desktop/ms685100(v=vs.85).aspx

    You do not set or query the value in the 3rd column. You set/query the value in the 2nd column. It is always -15..15 no matter what value you have in the first column.

    EDIT: the value in the 3rd column is the result of the combination of the first and second column. This is done by the system, not by yourself. You can not set the value in the 3rd column, and AFAIK you can also not retrieve it. But it doesn't matter because you only care about 1st and 2nd column. The rest is done by the system.


    Armin



    Friday, April 13, 2012 7:46 PM
  • http://msdn.microsoft.com/en-us/library/windows/desktop/ms685100(v=vs.85).aspx

    You do not set or query the value in the 3rd column. You set/query the value in the 2nd column. It is always -15..15 no matter what value you have in the first column.

    EDIT: the value in the 3rd column is the result of the combination of the first and second column. This is done by the system, not by yourself. You can not set the value in the 3rd column, and AFAIK you can also not retrieve it. But it doesn't matter because you only care about 1st and 2nd column. The rest is done by the system.


    Armin



    If I set my process to realtime and my thread priority to time critical it should give me a base priority of 31.

    It doesn't.

    You programmatically set your thread/process priority and then windows will automatically combine the values and form your base priority.

    Obviously, SetThreadPriority isn't working.

    Friday, April 13, 2012 8:20 PM
  • If I set my process to realtime and my thread priority to time critical it should give me a base priority of 31.

    It doesn't.

    As there is no function to return a thread's base priorty (the 3rd column), you can not make such a statement.

    Are you're still referring to Process.BasePriority? It's obviously process specific whereas a thread's base priority is thread specific. You can not use that value to make a statement about the base priorty of a certain thread. Process.BasePriority has a different meaning. As I said, just ignore it, or read the documentation about it.


    Armin

    Friday, April 13, 2012 8:46 PM
  • So what you're saying is, the code you provided works fine?

    Just use ThreadBeginAffinity?

    Friday, April 13, 2012 9:01 PM
  • You don't have to call the API to set the priority. Just set the priority property of the Thread object:

    System.Threading.Thread.CurrentThread.Priority = Threading.ThreadPriority.AboveNormal

    You must not pass the managed thread ID to SetThreadPriority as it expects a thread handle. That's something different. In theory, the runtime can change the system thread in which the managed thread is running, that's why there is a managed thread ID that does not change. If you wanted to call SetThreadPriority for the current thread you'd have to call API function GetCurrentThread() before, and before that, you'd have to call Thread.BeginThreadAffinity. Long story short:

    Imports System.Threading
    Imports AZ.Win32
    
    Public Class Main
    
       Shared Sub Main()
    
          Thread.BeginThreadAffinity()
    
          Try
             Dim h = Functions.GetCurrentThread()
             Functions.SetThreadPriority(h, AZ.Win32.ThreadPriority.AboveNormal)
             '...
             Functions.SetThreadPriority(h, AZ.Win32.ThreadPriority.Normal)
          Finally
             Thread.EndThreadAffinity()
          End Try
    
       End Sub
    
    End Class

    I don't post the declarations as it's only for demonstration.

    But again, API calls are not required.

    Don't mix up the priority class of the process with the priorty of the thread. By setting the latter you don't change the former. Both values are combined for each thread to the base priority as shown in the table in the link I posted some messages ago. The property CurrentProcess.BasePriority is something different again. Actually I haven't come across it yet. As it's readonly, just stick with the thread's Priority property and the PriortyClass property of the process object.


    Armin

    Dim CurrentThread = System.Threading.Thread.CurrentThread

    Public Sub Main()

    Thread.BeginThreadAffinity

    'Code

    Thread.EndThreadAffinity

    End Sub

    Private Sub Button1_Click

    'Create New Thread

    'Start Thread

    SetThreadPriority(CurrentThread, ThreadPriorityLevel.TimeCritical)

    End Sub

    I can't get this working at all. ThreadAffinity works fine, but setting the thread priority to Time Critical won't work. :(

    Can someone please post the code, so I can get past this...

    Saturday, April 14, 2012 3:22 PM
  • How do you know it fails? Did you check the return value of SetThreadPriority? If it's true, it succeeded. Did you call GetThreadPriority afterwards? If it returns TimeCritical you also know that it was successful. Just tried it and it works.


    Armin

    Saturday, April 14, 2012 5:16 PM
  • How do you know it fails? Did you check the return value of SetThreadPriority? If it's true, it succeeded. Did you call GetThreadPriority afterwards? If it returns TimeCritical you also know that it was successful. Just tried it and it works.


    Armin


    Post the code that you tried?
    Saturday, April 14, 2012 6:01 PM
  • Almost the same I already posted.

    Imports System.Threading
    Imports AZ.Win32
    
    Public Class Main
    
       Shared Sub Main()
    
          Thread.BeginThreadAffinity()
    
          Try
             Dim h = Functions.GetCurrentThread()
    
             If Functions.SetThreadPriority(h, AZ.Win32.ThreadPriority.TimeCritical) Then
                Try
                   MsgBox(Functions.GetThreadPriority(h).ToString)
                Finally
                   If Not Functions.SetThreadPriority(h, AZ.Win32.ThreadPriority.Normal) Then
                      Throw New System.ComponentModel.Win32Exception("Error at SetThreadPriority")
                   End If
                End Try
             Else
                Throw New System.ComponentModel.Win32Exception("Error at SetThreadPriority")
             End If
          Finally
             Thread.EndThreadAffinity()
          End Try
    
       End Sub
    
    End Class


    Armin

    Saturday, April 14, 2012 7:45 PM
  • This is my code. I don't get any errors running it, but I don't know if the thread priority is actually time critical:

    Imports System.Threading

    Private Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As IntPtr, ByVal nPriority As Integer) As Integer

    Private Declare Function GetCurrentThread Lib "kernel32" () As IntPtr

    Dim CurrentProcess As Process = Process.GetCurrentProcess

    Dim Thread As System.Threading.Thread
        Dim CurrentThread = GetCurrentThread()

    'Form Load
    CurrentProcess.PriorityClass = ProcessPriorityClass.RealTime

    'Button Click
     Thread = New System.Threading.Thread(AddressOf Sub)
            Thread.Start()
            SetThreadPriority(CurrentThread, ThreadPriorityLevel.TimeCritical)

    'Sub
     Thread.BeginThreadAffinity()

    'My Code

    Thread.EndThreadAffinity()

     Is this the way I'm supposed to set it up? Btw, thank you for all your time in helping me do this. I can promise you that I would have never found out how to work this stuff without your help. :)

    Saturday, April 14, 2012 8:51 PM
  • This is my code. I don't get any errors running it, but I don't know if the thread priority is actually time critical:

    Call GetThreadPriority to find it out.

    Your code sets the priority of the current thread, not of the started thread.

    This is my last post about this issue.


    Armin

    Saturday, April 14, 2012 9:36 PM
  • You never posted how to change the priority of the started thread.

    And, how am I supposed to get it right, if you're using your own functions in your examples? I'm just back at square one.


    I just want a time_critical thread. (My thread)
    • Edited by jonny_mando Saturday, April 14, 2012 10:43 PM
    Saturday, April 14, 2012 10:42 PM
    • "You never posted how to change the priority of the started thread."

    Right, because you last example was the first one that was meant to set the priority from the starting thread, not within the started thread. The code in your previous message containing code wasn't compilable so I ignored it and posted my code (that you've asked for).

    • "And, how am I supposed to get it right, if you're using your own functions in your examples?"

    Can't I suppose you've declared your own functions and use them? How else could you have ran your tests? Howe can you say it doesn't work if you don't have functions to call?

    • "I just want a time_critical thread."

    Call SetThreadPriority and pass TimeCritical. The answer won't change. Neither you believe me that it works, nor you trust the return value of SetThreadPriorty, nor the return value of GetThreadPriority, so what can I add?


    Armin

    Sunday, April 15, 2012 12:22 AM
    • "You never posted how to change the priority of the started thread."

    Right, because you last example was the first one that was meant to set the priority from the starting thread, not within the started thread. The code in your previous message containing code wasn't compilable so I ignored it and posted my code (that you've asked for).

    • "And, how am I supposed to get it right, if you're using your own functions in your examples?"

    Can't I suppose you've declared your own functions and use them? How else could you have ran your tests? Howe can you say it doesn't work if you don't have functions to call?

    • "I just want a time_critical thread."

    Call SetThreadPriority and pass TimeCritical. The answer won't change. Neither you believe me that it works, nor you trust the return value of SetThreadPriorty, nor the return value of GetThreadPriority, so what can I add?


    Armin

    Why can't you just post fully-working code? It would have saved us days of time...

    It's much easier to learn by reading example code then it is to google how to do things.

    Sunday, April 15, 2012 3:34 AM
  • I did post full working  code. You already have the declarations, so just use them. Your problem aren't missing declarations, it's a lack of acceptance.

    Armin

    Sunday, April 15, 2012 10:17 AM