|
Here I post some sample codes:
// myDlg class (class A)
CSocketSvr mySocket; // my socket variable
// ActiveX event handler
void myDlg::OnReceivedData(LPCTSTR ReceivedData)
{
...
// I start my thread - it will execute for the first time I receive any data
if (TRUE)
{
pThread = (CWzdThread*)AfxBeginThread(RUNTIME_CLASS(CWzdThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
}
...
}
// my method
void myDlg::Check()
{
if (mySocket.GetConnectedStatus()) <--- THIS IS WHERE IT GIVES ME THE RUNTIME ERROR
{
...
}
}
/////////////////////////////////////////
// CWzdThread class (class B)
myDlg* pDlg;
int CWzdThread::Run()
{
while(TRUE)
{
...
pDlg->Check();
}
}
///////////////////////////////////////////////
// CSocketSvr class (the activeX control class)
// to get the connection status
bool CSocketSvr::GetConnectedStatus()
{
....
return result;
}
I think this will give you enough information about what I am trying to do here.
mmm I still don't get it where should I add the Lock function.
Can you please explain more on that.
Thank you so much for your help.
FM
|
|
|
|
|
Hi,
this is assuming that the variable *pDlg has been initilized
to be equal to the this pointer of myDlg when it is created ,
in the oninitdialog or so
If the mySocket variable exist the error should happen
somewhere here
bool CSocketSvr::GetConnectedStatus()
{
....
return result;
}
if it does not exist, then it should not have been called at all unless you have multiple threads accessing the same instance.
on the other hand it could be that you are accessing the same variable while it is being used. (the case each time new data is being received)
so you must call the lock before you call any operation on the variable.
Here is a simple way without using MFC
1) Declare critical section
CRITICAL_SECTION g_CriticalSection;
...
..
..
2) When Program Starts, initilize the critical section
::InitializeCriticalSection (&g_CriticalSection);
3) When the program terminate, delete the critical section
::DeleteCriticalSection (&g_CriticalSection);
4) Whenever you are accessing mySocket
::EnterCriticalSection (&g_CriticalSection);
if (mySocket.GetConnectedStatus()) <--- THIS IS WHERE IT GIVES ME THE RUNTIME ERROR
{
..
}
::LeaveCriticalSection(&g_CriticalSection);
It is Illogical to define an inventor by his invention
Cheers
Alfadhly
|
|
|
|
|
mmm.. it seem it doesn't work with the critical section.
Still give me the same error.
Don't know why thread still cannot call the activeX control methods?
FM
|
|
|
|
|
Hi Farah,
Does that mean you have already initilized the *pDlg to point to an instance of MyDlg?
Cheers
It is Illogical to define an inventor by his invention
|
|
|
|
|
Yupp. I already assign *pDlg to point to an instance of myDlg.
FMansor
|
|
|
|
|
I hope you don't mean this line
myDlg* pDlg;
Cheers
It is Illogical to define an inventor by his invention
|
|
|
|
|
Yupp. That's what I did. But before I resume the thread, I assign this (which belongs to dialog class) to pDlg.
This is my code:
in dialog class:
void CMyDlg::OnDataArrival()
{
....
pThread = (CWzdThread*)AfxBeginThread(RUNTIME_CLASS(CWzdThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
pThread->MainDlg(this);
pThread->ResumeThread();
...
}
in thread class:
void CWzdThread::MainDlg(CMyDlg* spDlg)
{
pDlg = spDlg;
}
int CWzdThread::Run()
{
...
pDlg->Check();
...
}
Hope I do not confuse you.
TIA
FM - I can't remember my username
|
|
|
|
|
Hello, I am looking for some code that will produce consecutive sums for instance 5 is 2+3, 1+4, 0+5
If you have a good idea.. please let me know as to i cannot understand my teacher.. thank you
karen
|
|
|
|
|
Couldn't you just set one variable, call it X, to equal the value, an other, call it Y to equal zero, then set up a loop for Y that increases to the starting value of X. Then in each iteration you subtract one from X, and you'll always have two values that add up to the one you want.
I'm loathe to post code because I wouldn't want to encourage you to copy it without understanding it, but if you have a shot at it and post your code I'd be happy to go over it and make suggestions.
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
ok ill post the code in the morning..
Thanks
|
|
|
|
|
ok here is my partial code
#include <iostream>
using namespace std;
int main()
{
int i,x,input,j,sum;
cout <<"please enter a number"<<endl;
cin>> input;
//while (input>-1)
{
for (i=1; 1<=input; i++)
{
for (j=i;j<=input;j++)
{
sum=sum+j;
}
}
}
cout <<"Sum"<
|
|
|
|
|
FWIW I'll start by telling you to do a less than or greater than sign into the forum you need to use shift-7 (ampersand) followed by gt or lt. If I just type what I just explained I get < >, but typing in the symbols gets stripped as an HTML tag. Also typing <pre> and </pre> allows you to format the code as you see below.
You're on the right track, but I thought you wanted to output actual sums, as in 1+6=2+5=3+4...=7 ?
To do that you will need to do this:
int value1 = input;
for (int value2 = 0; value2 <= input; ++value2, --value1)
{
cout << value1 << " + " value2 << " = ";
}
cout << input << endl;
So what I am doing is maintaining a count from 0 to the input number, and another from the input number to 0. Adding one to one while removing one from the other means they will always add up to equal 'input', and so I can print the sum in a loop, finishing with the whole value.
I didn't test this code, so LMK if it doesn't work, or if there is something you do not understand (I'm not terribly good at explaining things ).
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
nono it has to be consecutive sums..
input=11
1+2+3+4+5=15
2+3+4+5=14
3+4+5+6=12
4+5+6=14
5+6=11
you have to increase it by 1 each time is which i have done
but what should i place after sums=sums+j ??
to make sure that after it finds the nubers added and it went over 11 to check again?
|
|
|
|
|
Ah... I get it now.
Does it have to be a sequence of a particular maximum size ? Do you have to approach the problem this way ? I'd approach it by saying if I use a sequence of n numbers to add up to x, then x%n = n-1, so that's what I would look for within a loop. To do it the way you are seems to me more complex, although it may be the way your teacher wants you to do it for the sake of the exercise.
LMK if you must do it the way you are, I'm going to have a play with it now.
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
Assuming you're allowed to do it this way, here is a solution:
#include "stdafx.h"
#include <iostream>
#include <math.h>
using std::cout;
using std::cin;
using std::endl;
int main(int argc, char* argv[])
{
int input;
cout <<"please enter a number ";
cin >> input;
for (int i = 2; i < 10; ++i)
{
if (input%i == i-1) break;
}
if (i == 10)
{
cout << "No solution found\n";
return 0;
}
int lowest = floor(input/i);
for (;i>=0;--i)
{
cout << lowest++;
if (i > 0) cout << " + ";
}
cout << " = " << input << endl;
return 0;
}
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
Damn - this does not work. Give me a minute...
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
This will work but has IMHO ceased to be elegant.
#include "stdafx.h"
#include <iostream>
#include <math.h>
using std::cout;
using std::cin;
using std::endl;
int main(int argc, char* argv[])
{
int input;
cout <<"please enter a number ";
cin >> input;
for (int i = 2; i < 10; ++i)
{
if ((input%i) == (i-1)) break;
}
if (i == 10)
{
cout << "No solution found\n";
return 0;
}
int lowest = floor(input/i);
if (lowest < 1) lowest = 1;
int sequence = 0;
for (;i>0;--i)
{
sequence += lowest;
if (sequence > input) break;
cout << lowest++;
if (i > 1 && sequence + lowest <= input) cout << " + ";
}
cout << " = " << input << endl;
return 0;
}
I'm at work so I can't devote any more time to it, but hopefully you can see what direction I'm heading. Right now I am managing cases my algorithm won't catch after the fact, which is *bad*. Maybe you can see where I have gone wrong. Try putting 6 into the first code I posted and you'll see it does not work, and it's pretty clear why. I just can't quickly wrap my head around itwhile other people are waiting for me to do some work so they can also continue with theirs.
If you don't get it sorted today I'll be happy to play with it more tonight.
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
Here we go - this works every time and uses while loops to get to where it's going.
#include "stdafx.h"
#include <iostream>
#include <math.h>
using std::cout;
using std::cin;
using std::endl;
int main(int argc, char* argv[])
{
int input;
cout <<"please enter a number ";
cin >> input;
int result = 1;
int low = 1, high = 1;
while (result < input)
{
result += ++high;
}
while (result != input)
{
while (result > input)
result -=low++;
while (result < input)
result += ++high;
}
for (int i = low; i < high; ++ i)
{
cout << i << " + ";
}
cout << high << " = " << result << endl;
return 0;
}
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
I don't see it listed in any of the project settings.
Cathy
|
|
|
|
|
It's not in the settings. Source help files are visible in the FileView, just like .cpp's and .h's.
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
Thanks!
Cathy
|
|
|
|
|
I have problem to implement a flashwindow within timer
method, somebody have a silimar code to flashwindow
like "MSN Messenger" when arrived a message.
thanks! bye
|
|
|
|
|
|
Hi,
I've got a question about non-type template parameters. I'm passing a global char* as a parameter to a template (in addition to other stuff). Something like this:
In RecordTemplate.h
template<class record_type,="" char*="" record_name="">
RecordTemplate
{
// Methods and other neat stuff go here
};
Then in Record1.h
#include "RecordTemplate.h"
// Declare the string literals for record name
char _Record1RecordName[];
// Declare the struct type for use with the Adi record
struct Record1Struct
{
double TimeDate;
// other stuff . . .
};
// Typdef a specialization
typedef RecordTemplate<record1struct, _record1recordname=""> Record1;
And then in Record1.cpp
// Define the record name
char _Record1RecordName[] = "The name of the record";
This is working OK, and I am able to stamp out specializations of the template class. But, I want to be able to declare the char arrays as const, and initialize them in the declaration, like this (in the header file):
const char _Record1RecordName[] = "The name of the record.";
Then, I don't need the extra source file, and the records are safe from modification. I tried to use the const keyword, and I get a strange compiler error about template parameters may not be static. Apparently the const keyword affects the linkage, but I became a little confused at that point.
Has anyone tried this before? Basically, I just want to be able to pass a global const array of characters (or any other sort of const array for that matter) as a template parameter.
Thanks,
Aaron
|
|
|
|
|
Const objects have internal linkage by default (meaning the object is only visible in that translation unit [.CPP file]). Template params cannot have internal linkage.
You may be able to fix the error by adding extern to the declaration, or putting the array in an unnamed namespace.
Also, check out my C++ FAQ - I just added a link to a template FAQ that covers this particular issue.
--Mike--
http://home.inreach.com/mdunn/
Ford: How would you react if I said that I'm not from Guildford after all, but from a small planet somewhere in the vicinity of Betelguese?
Arthur: I don't know. Why, do you think it's the sort of thing you're likely to say?
|
|
|
|