|
Thanks you
for that (right) scenario - WaitForSingleObject(),
SuspendThread();
may be better be used.
Or system timer - does it immediately can be freed?
|
|
|
|
|
I used SetTimer() right before I call SuspendThread() to make the function sleep.
void CSleeperThread::run()
{
UINT_PTR pTimer = SetTimer(NULL, NULL, job.Schedule.GetNextRunTime(), TimerProc );
SuspendThread();
}
In SuspendThread() I pass a call back function called ThreadProc().
I've implemented ThreadProc() I use ResumeThread() and KillTimer() like so:
void CSleeperThread::TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
::EnterCriticalSection(&lock);
KillTimer(NULL, idEvent);
if(idEvent == pTimer)
ResumeThread();
::LeaveCriticalSection(&lock);
}
I used critical sections because in my class there's a possiblity that ThreadProc could be called again, before it's finished processing the first time. It may be overkill, I haven't tested that part yet.
Mike Ellertson
|
|
|
|
|
Thanksyou for response.
You of course right applied CriticalSection -
no one must relay on thread sequencies assumptions.
error may happen on whatever unpredictable situation.
So using this is a must.
___________
But about timer - did you test how fast this timer is killed?
In comparison with WaitForSingleObject()timing.
This is a main issue here...
Please compare effectivenes.
|
|
|
|
|
I haven't tested yet how fast the KillTimer() executes. You could be right, it may be a performance issue.
I found another solution to the static requirement on the callback function. I found that if I declared a global function like this:
VOID CALLBACK functionName(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
Then passed it to to SetTimer() as the fourth parameter like this:
uiPtr = SetTimer(NULL, NULL, uiSeconds, TIMERPROC(functionName));
The compiler allows me to pass a pointer to the callback function without declaring it as static. This is great, because then I can access non-static variables and function. Just a note, make sure you type case the functionName as TIMERPROC otherwise this won't work.
The problem I'm having now, and here's where I need some input, is I want to pass a member function to a class as my callback function. So here's what I'm trying to do:
VOID CALLBACK CSomeClass::functionName(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime) {
}
And then I try and pass this function pointer to SetTimer like so:
CSomeClass::otherFunction() {
UINT_PTR uiPtr;
uiPtr = SetTimer(NULL, NULL, uiSeconds, TIMERPROC(functionName));
}
That's when the compiler complains. Basically it says that it can't convert from void (__stdcall*)(CSomeClass::*functionName)(arglist...)
to void (__stdcall*)(*functionName)(arglist...)
so what I'm getting from this error is, SetTimer() is meant to be used on global functions only. There is a class impelementation of CWnd::SetTimer(). Has anyone used this and do you know if it will solve the above problem?
Thanks in advance,
|
|
|
|
|
Converting from global(static) calling conversions to this-call,
van be made by using Boost library templates.
Find on the www this very widely used library.
___________
Problem not in direct performance but in delays that can cause killtimer,
to up to period of timer
that is already big performance - die.
|
|
|
|
|
Hi all,
New to programming, writing my first app using TCP.
I need to make a client server that will send messages to a server. Right now i have two dialog windows. One that takes in the IP that the user wants to connect to(Connection), and another window where the user will set the information that will be sent (Format_Msg). I would like to have a third window that tells me what is sent over and what it receives.
I can get the app to connect to the server, I just can not send the appropriate messages over to the server. I do all the connecting and instantiate a socket called ConnectServer in the Connection class. I do all the formatting of the message in the Format_Msg class.
my question is how do I send these messages now? I am trying to use the:send (SOCKET s, const char FAR * buf, int len, int flags) command but don't know what I am doing wrong.
Can I use the same connection that I made (ConnectServer), or do I have to instantiate a socket in the Format_Msg class too? If I don't, could you please tell me how I could get it to recognize the other socket that was instantiated in the Connection class?
If you have code to how i could do this, it would be great.
Many thanks in advance.
|
|
|
|
|
|
Hi,
I read those articles, thats how i got the app to connect to the server.
I am still having problems sending the information though. I don't know if I should be passing in the same connection or instantiating a new one in the second class. Don't know how to use the same connection actually. Can anyone help me with that?
Thanks in advance.
|
|
|
|
|
you mean you've opened a tcp connection to a server and want to send data through?
easy...
send( tcpsocket, buffer, len, 0 );
Don't try it, just do it!
|
|
|
|
|
yes that is what i want to do.
Only thing is that I create the socket in one class and want to format a message before I send it, which is done in another class. I do not know how to get the same socket that I created in the first class to be used in the second class. If i try and use the same socket, it will give me an error saying that it is a undeclared identifier.
Thanks for your help. I know it is probably a simple question, but I am a newbie with program. (Trying to change my career and learn something new)
|
|
|
|
|
I presume that you are declaring an instance of the "formatting" class inside the class with the "working" socket???
If you are just add a socket variable to the formatting class and overload the constructor of the formatting class so that when you are creating the formatting class object just do somerhign like this:
MyFormatClass mfc = new MyFormatClass(socket);
Or else you could make the socket variable in the format class public and just set it like:
MyFormatClass mfc;<br>mfc.socketvar = this.socket;
But without knowing more about your code and the way your classes work/interact I can't give a definite answer.
Regards,
Brian Dela
http://www.briandela.com[^]
|
|
|
|
|
Thanks for the help Brian.
I am really new to programming, and do not understand how to do what you are telling me.
How would I declare an instance of the "formatting" class inside the class with the "working" socket?
What other information do you need to know about my program?
Many thanks in advance.
|
|
|
|
|
You said you have a class that formats the string, yeah? ... Unless it is a static function within the class that you are using (i.e. you would be doing something like MyFormatClass::FormatString(string); .
Otherwise you have to do something like:
MyFormatClass mfc;
or
MyFormatClass* mfc = new MyFormatClass()
Are you doing anything like this???
If you want, email me on the code.
Regards,
Brian Dela
http://www.briandela.com[^]
|
|
|
|
|
Hi
I´m new to C++ and working with passing files back and fourth to a webservice. I have found som code in C# that does this for me but I can´t convert it into C++. Can someone please help me with the two last lines?
DimeAttachment attachment = new
DimeAttachment("application/octet-stream", TypeFormatEnum.MediaType, ms);
HttpSoapContext.ResponseContext.Attachments.Clear();
HttpSoapContext.ResponseContext.Attachments.Add(attachment);
Regards
// Daniel
|
|
|
|
|
Do u know a func. like scanf() which works in C's graphic mode? ( I can't position the cursor with gotoxy() command while using scanf() func in graphic mode.)
note:i am using Borland TURBO C++ v.3.0
|
|
|
|
|
sscanf
Don't try it, just do it!
|
|
|
|
|
I need to use the sqrt() function in <cmath> in one of my functions in a Windows program using MFC. At the top of the file I put in these lines:
#include <cmath> <br />
using std::sqrt;
and I got this error:
error C2653: 'std' : is not a class or namespace name
Why doesn't the compiler recognize the std namespace?
|
|
|
|
|
It's odd that it does not work. As a workaround, use the C-standart function.
<br />
#include <math.h><br />
...<br />
double x = sqrt (y);<br />
Best regards,
Alexandru Savescu
P.S. Interested in art? Visit this!
|
|
|
|
|
Thanks, I did that.
I'm using VC++ 6.0 if that helps.
|
|
|
|
|
Try:
#include <cmath>
using namespace std;
...
// use the sqrt func
float x = sqrt(y);
or you can just call the sqrt function without the "using" keyword:
#include <cmath>
...
// use the sqrt func
float x = std::sqrt(y);
HTH.
David Hisel -- http://www.hisel.com/
|
|
|
|
|
I tried both of those, but as expected, I get the same error--my compiler does not recognize "std" as a namespace.
My compiler does recognize "std" as a namespace for win32 console apps, but not this, my first win32 with MFC app attempting to use the std namespace.
|
|
|
|
|
The things I will say in this reply are MS Visual C++ 6.0 specific. I have not tried them on any other compiler.
7stud wrote:
My compiler does recognize "std" as a namespace for win32 console apps
You probably use #include <iostream> , which includes the std namespace. But, the cmath header file does not contain the std namespace. In fact, the following code is perfectly legal:
#include <cmath>
int main() {
double x = sqrt(5);
}
But this one is not:
#include <cmath>
using std::sqrt;
So you don't need to use the std namespace if you are going to use cmath in Visual C++ 6.0.
Hope this helps.
Hosam Aly Mahmoud
|
|
|
|
|
Thanks for the explanation.
|
|
|
|
|
You're welcome.
Hosam Aly Mahmoud
|
|
|
|
|
How do you get the current time of the machine in milliseconds.
I have used the time() function from time.h before but that only gives me back up to the seconds..
Any ideas would be great - thank you
|
|
|
|