Page 1 of 1
CreateThread() then, threadpriority() = "not a thread"
Posted: Thu May 12, 2011 6:41 am
by jassing

(for those that want code)
Code: Select all
hThread = CreateThread( @FetchImage(), *offender )
If IsThread(hThread)
ThreadPriority(hThread,#pb_thread_below_normal)
EndIf
I know this is not a usable code snippet -- but... randomly, I'm getting "not a thread" when I try to adjust the thread priority.
I have added "Delay(250)" at the very start of FetchImage() to be sure it's not exiting too fast.
I then added the "if IsThread()" bit -- and I"m still getting "not a thread"
Any ideas on why or how to rectify this?
Re: CreateThread() then, threadpriority() = "not a thread"
Posted: Thu May 12, 2011 7:26 am
by idle
you need to use api's to get the handle
Code: Select all
;get priority
PriorityClass = GetPriorityClass_(GetCurrentProcess_());
Priority = GetThreadPriority_(GetCurrentThread_());
;set priority
SetPriorityClass_(GetCurrentProcess_(), #REALTIME_PRIORITY_CLASS);
SetThreadPriority_(GetCurrentThread_(), #THREAD_PRIORITY_TIME_CRITICAL);
;reset to normal
SetThreadPriority_(GetCurrentThread_(),Priority);
SetPriorityClass_(GetCurrentProcess_(), PriorityClass);
;lock to core 1
mask.i=1
If Not IsThread(gx)
SetThreadAffinityMask_(GetCurrentThread_(),mask)
gx = CreateThread(@CheckFreqency(),CheckInterval)
EndIf
Re: CreateThread() then, threadpriority() = "not a thread"
Posted: Thu May 12, 2011 8:04 am
by jassing
Thanks -- I'm confused tho -- so the handle returned by CreateThread() can't be used with other functions reliably?
do you know the why of the issue?
In addition to being confused, I'm lost -- that code doesn't seem to do anything after hte createthread() -- which is what I want to do -- is have a handle for the thread that I just created... that code (it's late here, so...) -- seems to handle getting the thread of the "current thread" -- no one just created....
Re: CreateThread() then, threadpriority() = "not a thread"
Posted: Thu May 12, 2011 10:06 am
by infratec
Hi,
I think idle had missunderstood what you mean.
He showed you how to set the priority of the own thread.
But if you want to change the priority of your created thread, your commands are correct.
But I have no explanation why it fails sometimes in your code.
Bernd
Re: CreateThread() then, threadpriority() = "not a thread"
Posted: Thu May 12, 2011 10:29 am
by cxAlex
Maybe your Thread is terminated (finished) between IsThread() and ThreadPriority()? I don't know your thread or how long it is, but it could happen.
Greets, Alex
Re: CreateThread() then, threadpriority() = "not a thread"
Posted: Thu May 12, 2011 11:38 am
by c4s
Code: Select all
hThread = CreateThread( @FetchImage(), *offender )
If hThread
;...
...This should be enough anyway.
Re: CreateThread() then, threadpriority() = "not a thread"
Posted: Fri May 13, 2011 1:02 am
by netmaestro
cAlex wrote:Maybe your Thread is terminated (finished) between IsThread() and ThreadPriority()? I don't know your thread or how long it is, but it could happen.
This gets my vote.
c4s wrote:...This should be enough anyway.
No, remember that the thread executes asynchronously. After satisfying "If hThread.." successfully the thread could already be gone by the time ThreadPriority tries to execute.
Re: CreateThread() then, threadpriority() = "not a thread"
Posted: Fri May 13, 2011 7:04 am
by infratec
Hi,
that was also my first impression.
But than I read:
jassing wrote:I have added "Delay(250)" at the very start of FetchImage() to be sure it's not exiting too fast.
So he had checked this already.
Bernd
Re: CreateThread() then, threadpriority() = "not a thread"
Posted: Sat May 14, 2011 7:43 am
by JustinJack
I think you may need to call ThreadID() to get the OS handle of the thread you created..
Code: Select all
numThread = CreateThread(@misc(), *lParam)
hThread = ThreadID(numThread)
SetThreadPriority_(hThread, #THREAD_PRIORITY_HIGHEST)
PB Help on ThreadID()
Returns the unique ID which identifies the given 'Thread' in the operating system. This function is very useful when another library needs a thread reference.
This result is sometimes also known as 'Handle'. Look at the extra chapter Handles and Numbers for more information.