10-18-2006 01:52 PM
10-18-2006 04:08 PM
Here is the code you can try. I feel the Dealy (0.1) in main thread is the reason for causing the problems. I just don't know why and how.
#include <formatio.h>
#include <dataacq.h>
#include <utility.h>
#include <ansi_c.h>
#include <cvirte.h>
#include <userint.h>
#include "real time.h"
static int panelHandle, tsqHandle;
int CVICALLBACK WriteQueue (void *functionData);
int threadID, j;
int stopFlag, plotFlag;
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "real time.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}
int CVICALLBACK Start (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int number, readID, i;
switch (event)
{
case EVENT_COMMIT:
CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, WriteQueue,
0, &threadID);
stopFlag = 0;
plotFlag = 0;
j = 0;
while (1)
{
if (stopFlag == 1) break;
Delay (0.1);
SetCtrlVal (panel, PANEL_Z, (double) j++);
ProcessSystemEvents ();
}
CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE, threadID, 0);
CmtReleaseThreadPoolFunctionID (DEFAULT_THREAD_POOL_HANDLE, threadID);
break;
}
return 0;
}
int CVICALLBACK Stop (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
stopFlag = 1;
break;
}
return 0;
}
int CVICALLBACK WriteQueue (void *functionData)
{
double dsp;
char read_string [300];
// while (1) if (stopFlag == 1) break; /*This is a correct one to show mulithread working.*/
while (1)
{ /*Frozen1: Z can not go beyond 100*/
if (stopFlag == 1) break;
Delay (0.1);
}
// while (1)
// { /*Frozen2: Z can not go beyond 100*/
// if (stopFlag == 1) break;
// Timer ();
// }
// while (1) /*Forzen3: Z can not go beyond 200.*/
//{
// if (stopFlag == 1) break;
// strcpy (read_string, "123");
// Fmt (&dsp, "%f<%s", read_string);
// }
return 0;
}
10-19-2006 04:36 PM
10-19-2006 04:43 PM
Hello,
Did you really try to run the codes? I want to know your result. If same thing happens, I would like to know the solution for it. Thanks.
10-19-2006 04:51 PM
10-19-2006 04:55 PM
Thank you very much.
10-20-2006 12:23 AM
Hi,
I did run you code on CVI 6.0. It did not freeze, but it did error when I started a second thread and then try to stop a thread.
Have you looked at the examples for MultiThreading, that come with CVI.
Regards
Ray Farmer
10-20-2006 09:40 AM
Hello,
Thanks. That's what I want to demonstrate there are some Timer () related problems for multithread in CVI. When you just run the single while (1) loop, the code runs correct. However, if you replace that with the other commented codes, the computer will freeze.
Before I wrote the codes, I have already carefully studied many examples in CVI regarding mulithreading. I am using CVI7.1.
10-20-2006 10:37 AM
Well, I tried your code building a simple project without errors in none of your stated conditions: secondary tread has always been runnig counting up to 3000 and more... Nor I got any errors when stopping threads.
I am attaching my project for you to test.
System: Win XP-PRO with SP2 - CVI 7.1.1 (313)
10-20-2006 03:05 PM
Hello,
Thank you very much. I run your code in my computer. My computer froze again when Z hits 14. I can not stop it either. I am using CVI7.1
Thanks.