|
I would use a waitable timer. Because it will block your program while you are waiting, but if you expand your program in the future, you will have the ability to wake that thread up before the timer expires. With Sleep, your program will block for the same amount of time and this cannot be changed at runtime.
Here is an example from MSDN of how to use a waitable timer.
#include <windows.h>
#include <stdio.h>
int main()
{
HANDLE hTimer = NULL;
LARGE_INTEGER liDueTime;
liDueTime.QuadPart=-100000000;
hTimer = CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
if (!hTimer)
{
printf("CreateWaitableTimer failed (%d)\n", GetLastError());
return 1;
}
printf("Waiting for 10 seconds...\n");
if (!SetWaitableTimer(
hTimer, &liDueTime, 0, NULL, NULL, 0))
{
printf("SetWaitableTimer failed (%d)\n", GetLastError());
return 2;
}
if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\n", GetLastError());
else printf("Timer was signaled.\n");
return 0;
}
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
Hi,
I need to implement a SMTP server in my appl. Does anyone here have done this? Please, any source codes or ideas would be very useful.
Thank u very much,
Dirso.
|
|
|
|
|
An SMTP server? Or do you mean an SMTP proxy, that pushes the SMTP chat to the real SMTP server? If so, you can use my SMTP proxy class. It's a non-MFC C++ class.
http://www.codeproject.com/useritems/nishcsmtpproxymt.asp
Nish
The posting stats are now in PDF:-
http://www.busterboy.org/codeproject/
Feel free to make your comments.
Updated - May 04th, Saturday
|
|
|
|
|
Hi,
I do need to build a SMTP server. If u have any ideas, please let me know.
Thank u very much,
Dirso.
|
|
|
|
|
If you are willing to consider a commercial source code library, our Dundas TCP/IP library includes everything you'll need to generate an SMTP server (SMTP in, SMTP out, POP3, DNS resolution, account management, relay handling, etc., etc.)
Dundas TCP/IP Products
David
http://www.dundas.com
|
|
|
|
|
A commercial code could be used, but i don't need everything of ur library. If u have a cheaper solution or a trialware, please tell me.
Thank u very much,
Dirso.
|
|
|
|
|
Hi, everyone!
I encounter a problem here with multiple document & multiple view application. I use the MDI frame(with 3 different Document Templates)and I also attach an extra view to the first document class corresponding to the 1st document template (Use AddDocTemplate). Then I want this extra view to render something base on the current active view(actually base on the current active document). Is there anybody know how to implement this?
The dificulty I encounter here is that:
While I make modification on second document(belong to 2rd document template) inside the view class(belong to 2rd document), I call UpdateAllViews(this), then only the views belong to the 2rd document get the Update message and response to it. How to make the extra view which belong to the 1st document to get the update message?
Best Regards,
Wayne King
09 May 2002
|
|
|
|
|
If you notice the UpdateAllViews method is a member of the CDocument class. It will only send the update message to views attached to that instance of the CDocument object. If you want a more global update method, you can do it through the app like this:
void CMyApp::UpdateEverybody()
{
POSITION posTemplate;
POSITION posDocument;
posTemplate = GetFirstDocTemplatePosition();
while(posTemplate)
{
CDocTemplate* pTemplate = GetNextDocTemplate(posTemplate);
if(pTemplate)
{
posDocument = pTemplate->GetFirstDocPosition();
while(posDocument)
{
CDocument *pDoc = theApp.pDefectDocTemplate->GetNextDoc(posDocument);
if(pDoc)
pDoc->UpdateAllViews(NULL);
}
}
}
} In your code you make the call like this:
...
theApp.UpdateEverybody();
... I also like the declare the theApp object as extern so most of my code can see it. Add this one line to your apps header:
class CMyApp : public CWinApp
{
...
};
extern CMyApp theApp; There may be other and better ways to do this, maybe someone else will respond with other answers. This method has worked well for me in the past.
Hope this helps...
Jonathan Craig
www.mcw-tech.com
|
|
|
|
|
Hi, Jonathan Craig!
Thanks for your kind response. After play around with my codes some more,
I realized that the extra view should not belong to any perticular
document. This bring in one more problem here. How to send Update message
to the view without a document?(How to get the pointer of this view?)
I still use AddDocTemplate(pTemplate) & pTemplate->CreateNewFrame(NULL, NULL) to create the view, but pass the pDoc as a NULL pointer.
Any idea and comment are greatly appreciated!
Best Regards,
Wayne King
09 May 2002
|
|
|
|
|
I would just send all the windows a user defined message #define WM_MY_MSG (WM_USER + 1). Using the ON_MESSAGE macro to handle the mesage in the view. You can make this message handler do what every update is doing..
void CMyApp::SendMessageToEverybody(UINT nMsg, WPARM wParam, LPARM lParam)
{
} Hack on...
Jonathan Craig
www.mcw-tech.com
|
|
|
|
|
I have a Windows explorer type program, that talks to a printer via a serial link. The printer stores many files internally. When I drag a file across from the printer to Windows explorer, I need to somehow get Windows explorer to download this file.
Or I need to get information about where the files were dropped in explorer, so my app can start the download. ActiveSync does such a thing, so it must be possible!
I can handle it when explorer drops files on my app, but not vice versa.
Any help or advice is very welcome!
Thanks in advance,
Alan Dugdall
|
|
|
|
|
Hi All
Has anybody here communicated via VC++ to a chain of hardware devices via the RS232-485 converter?
If so any documentation regarding the comm. to the converter or any weblinks to the resources is greatly appreciated
Thanx in advance
Agnihothra
|
|
|
|
|
Yes and they work just fine. No real documentation required because they are pratically idiot proof. If I remember my specs right, all you usually need to do is support hardware flow control and then the 485 convert does the rest.
That is if I remember my 485 right. I never can remember which one of 485 and 422 is just a simple RX/TX and which has the extra control lines for who wishes to access the wire.
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
I am new at VC++ but i didnt understand the pointer thing.
Why we use them? Where we need them? Please,someone help me!I want to know ALL REASONS why we use them.Thanks to everyone.
|
|
|
|
|
I think different: why is not everything a pointer?
Pointers give more flexibility & efficiency. In some cases you want the value not the reference, there you (maybe) don't need pointers. But with at least bigger data stuctures you want back your pointers, e.g. copying data from/to stack is not optimal.
|
|
|
|
|
cause data has to be stored somewhere, the pointer has to point somewhere...
|
|
|
|
|
agreed
|
|
|
|
|
Hmm, well, it has many advantages. Of course I can't remember all of them. But here are some.
1. Stack of the program is limited, and it is used for regular variables. Ponters are limited only by amount of memory in the computer, not on the stack.
2. When you have a class for example, which uses for example 100Mb of the memory. Now passing the variable to another function will make a copy of the class and you will have 200Mb of memory usage. Instead of this you can pass a pointer, which holds only 4 bytes. Actually this one is same as ByRef in VB - reference (you have references in C++ too).
Well, can't remember more right now, but sure others will find you enough
Philip Patrick
Web-site: www.stpworks.com
"Two beer or not two beer?" Shakesbeer
Need Web-based database administrator? You already have it!
|
|
|
|
|
Try reading my article on pointers for an introduction to the subject, and feel free to make comments in the discussion area of the article.
--
Andrew.
|
|
|
|
|
I'm no expert but here's 4 reasons for using pointers
1. They allow functions to modify their calling arguments.
2. Data is more easily passed around ie passing 1 pointer is easier than passing a whole object.
3. Data may be more easily accessed, for instance when data is stored in an array a pointer allows you to move through the array in a loop to access each item.
4. They allow you to dynamically allocate memory, this is good because it is more flexible than fixing memory allocation when programming.
Hope that helps
ali p
|
|
|
|
|
Since you say you're new at VC++, what follows may make little sense to you, but hopefully you'll grasp the idea soon.
If you're into OOP, many objects will be derived from a common base. Usually, a piece of code uses such object without really knowing the particular class they belong to (only that they're derived from the base). In this situation, passing by value (without pointers) is impossible, since the code won't know in advance how much space will take the object (different derivations can have more or less members not belonging to the base), so no copying around can be performed. I this case, the only solution is to pass to the unknowing code a pointer to the actual location of the object, so preventing copying.
In Java and C# the situation is the same, but the language hides this to you by making implicitly every object variable a pointer to an object --so these languages have pointers but you can't see them.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
I've just read in a web site that using pointers have 3 main reasons:
Array Operations,Dynamic Memory(variable sized arrays and structs like linked lists) and Parameter Passing.I've read something about this polimorphism thing,too but i didn't understand any word
Now,are these 3 reasons enough,or is there something more?
|
|
|
|
|
It is a most bizarrely mocking fact that a lot of C++ programmers realize the worth and splendor of pointers only when they start using a language which does not support them directly.
Nish
The posting stats are now in PDF:-
http://www.busterboy.org/codeproject/
Feel free to make your comments.
Updated - May 04th, Saturday
|
|
|
|
|
Some people call C++ the mid-level language. What does "mid-level" mean? Not as high level as Java or VB is (everything you need is just out there: Java has Virtual Machine, and the VB development tool does a lot of things for programmers. ); and not as low level as assembly language is (push, mov, eax, ..... ).
As sort of mid-level language, C++ covers both features of high and low ones. C++ is a structured language and uses 47 ( +1 : 'typename' ) English keywords, and the syntax is close to a certain human speach (if... else. do... while. try...throw... catch... ).
Then, what feature C++ covers as low-level one? Direct access to memory! How? With the feature of pointers, we can.
For example, if you want to set some desired value to somewhere in the memory, we may use:
int* p = (int*)0x0FCC00;
*p = 123456;
Or you can view where the variable is, you can code such:
double dMyMagicNum = 3.1415926535897932384626433832795;
printf("dMyMagicNum is at 0x%p \n", &dMyMagicNum);
There is also a trick called "Placement memory allocation".
If you had allocated some memory in the free store:
char* buf = new char[sizeof(int) * 300];
int* p = new(buf + 6) int;
printf("Buf: 0x%p, p: 0x%p\n", buf, p);
Maxwell Chen
People say "No news is good news". Then, no code is good code!?
|
|
|
|
|
I'm working with a text box (CEdit), but unfortunatly, there aren't any functions to change the text color.
I would like to know what is the easiest way to make a text box with different lines of text in different colors. There are quite some syntax coloring edit boxes out there, but they have way too many features.
Even a rich edit control (Working in a dialog) doesn't seem to support this easily. (you have to select parts of the text or something, then you can change a mask...)
Maybe it can be done with OnCtlColor, but then I can only change the text color of the entire Dialog...
Got any ideas?
|
|
|
|
|