Threading - need help making callback function a thread

  • Thread starter Thread starter Jim
  • Start date Start date
J

Jim

I have a windows application that is accessing unmanaged
code and providing a callback function that will create
events at unknown time spans. When the EventHandler
(callback function) fires it causes my program to crash or
freeze, or do strange stuff. When I remove this callback
function it works great. Problem is I need this callback
function to implement my state machine for the program so
I think I need to encapsulate this callback function into
its own process, but it takes 4 parameters. I have looked
all over the web, but cannot find a good example of this
implementation can anyone point me in a good direction?
 
Jim,

What is the mechanism that you are passing? Are you passing a COM
interface pointer, or are you passing a function pointer (which is marshaled
from a delegate into unmanaged code)? Each way has its own considerations,
but both are certainly doable.
 
I am sending a fucntion pointer. here is my Event Handler
Method. What is happening, is some how when my
eventhandler is fired it will lock up my program. I think
there is some memory issues here and I can't figure it
out.

unsafe public void EventHandler (void* vpUserData, uint
uiEvent, uint uiParam0, uint uiParam1)

{


EvQ[iQIn].uiUserData = (uint)vpUserData;

EvQ[iQIn].uiEvent = uiEvent;

EvQ[iQIn].uiParam0 = uiParam0;

EvQ[iQIn].uiParam1 = uiParam1;

richTextBox1.AppendText("\r\nEvent Handler " +
iQIn.ToString());

richTextBox1.AppendText("\r\nEvent : " + uiEvent.ToString
());

iQIn ++;

if(iQIn == 200) //Queue at max compacity

{

iQIn = 0; //Reset to 0

}

if(uiEvent == 215)

{

if(uiParam0== TONE_DIALTONE)

{

richTextBox1.AppendText("\r\n WE HAVE DIALTONE \r\n");

}

if(uiParam0 == TONE_RINGBACK)

{

richTextBox1.AppendText("\r\n WE HAVE RINGBACK \r\n");

}

if(uiParam0 == TONE_BUSY)

{

richTextBox1.AppendText("\r\n WE HAVE BUSY \r\n");

}

}


}
-----Original Message-----
Jim,

What is the mechanism that you are passing? Are you passing a COM
interface pointer, or are you passing a function pointer (which is marshaled
from a delegate into unmanaged code)? Each way has its own considerations,
but both are certainly doable.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jim said:
I have a windows application that is accessing unmanaged
code and providing a callback function that will create
events at unknown time spans. When the EventHandler
(callback function) fires it causes my program to crash or
freeze, or do strange stuff. When I remove this callback
function it works great. Problem is I need this callback
function to implement my state machine for the program so
I think I need to encapsulate this callback function into
its own process, but it takes 4 parameters. I have looked
all over the web, but cannot find a good example of this
implementation can anyone point me in a good direction?


.
 
I'll bet that your callback is being called on a thread other then the UI
thread. I think you should be using this.Invoke on any calls to your UI
controls.

Jeffrey


Jim said:
I am sending a fucntion pointer. here is my Event Handler
Method. What is happening, is some how when my
eventhandler is fired it will lock up my program. I think
there is some memory issues here and I can't figure it
out.

unsafe public void EventHandler (void* vpUserData, uint
uiEvent, uint uiParam0, uint uiParam1)

{


EvQ[iQIn].uiUserData = (uint)vpUserData;

EvQ[iQIn].uiEvent = uiEvent;

EvQ[iQIn].uiParam0 = uiParam0;

EvQ[iQIn].uiParam1 = uiParam1;

richTextBox1.AppendText("\r\nEvent Handler " +
iQIn.ToString());

richTextBox1.AppendText("\r\nEvent : " + uiEvent.ToString
());

iQIn ++;

if(iQIn == 200) //Queue at max compacity

{

iQIn = 0; //Reset to 0

}

if(uiEvent == 215)

{

if(uiParam0== TONE_DIALTONE)

{

richTextBox1.AppendText("\r\n WE HAVE DIALTONE \r\n");

}

if(uiParam0 == TONE_RINGBACK)

{

richTextBox1.AppendText("\r\n WE HAVE RINGBACK \r\n");

}

if(uiParam0 == TONE_BUSY)

{

richTextBox1.AppendText("\r\n WE HAVE BUSY \r\n");

}

}


}
-----Original Message-----
Jim,

What is the mechanism that you are passing? Are you passing a COM
interface pointer, or are you passing a function pointer (which is marshaled
from a delegate into unmanaged code)? Each way has its own considerations,
but both are certainly doable.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jim said:
I have a windows application that is accessing unmanaged
code and providing a callback function that will create
events at unknown time spans. When the EventHandler
(callback function) fires it causes my program to crash or
freeze, or do strange stuff. When I remove this callback
function it works great. Problem is I need this callback
function to implement my state machine for the program so
I think I need to encapsulate this callback function into
its own process, but it takes 4 parameters. I have looked
all over the web, but cannot find a good example of this
implementation can anyone point me in a good direction?


.
 
Back
Top