|
Please, read the posting guidelines before posting. When posting code, use the pre tags to format your code properly.
Now to your question: there's no magic way to convert from a console application to a MFC application. You'll need to understand how MFC works and refactor parts of your code. What has to be modified highly depends on your specific application, there's no generic way to do something like that.
So, if you never worked with MFC before, I strongly suggest you start by reading a good book on the subject, it will make you gain a lot of time in the near future.
|
|
|
|
|
I'd second Carlo's suggestion (in spite of the fact that he can be an annoying prick at times).
Simply use the wizard to generate the MFC project of desired type (dialog, SDI, etc.,) and add code from your console application as applicable. Can't be easier than this.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Rajesh R Subramanian wrote: Carlo's suggestion (in spite of the fact that he can be an annoying prick at times).
Oh, thank you, my good friend...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
You're welcome!
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Created several Controls in my GUI, but all of them have Bold text.
How do you disable it, so Windows displays them as Regular text?
|
|
|
|
|
By default, your controls have the SYSTEM_FONT set as font to them, if you want to change that you should use WM_SETFONT[^] to give them a different font. See here[^] for some additional information, just look for SYSTEM_FONT on the page, and be sure to read the remarks section.
p.s: You can use CreateFont[^] and/or CreateFontIndirect[^] to create fonts.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
This does not happen spontaneously. What technology are you using, I mean what do you use to design your control? Next time try supplying some more information, your question's subject 'Win32/C++' is not exactly limpid.
|
|
|
|
|
Fareed Rizkalla wrote: Created several Controls in my GUI, but all of them have Bold text.
How do you disable it, so Windows displays them as Regular text?
Query:
1. Have you assigned any FONT object to your control? if YES, you need to look into it property.
IF not, check you window desktop display or ZOOM, that might be causing problem
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
So, here is the thing:
for example i got a function which reads a file line by line and outputs each line to a console.
My file got 10 lines of text. Now i want to implement multithreading. i can use, for example,
int total_nr_of_threads = 7;
create max up to 7 threads and each thread will be responsible for reading some line from text file starting from the very 1st line and outputs data. And then for example when 1st thread finishes, it checks which line hasnt been read and read it and then outputs data. So how can i implement this? How to check if some line has been already read by some thread and another thread will not read it again?
I'm confused about the algorithm, just got stuck with it
Thanks in advance.
|
|
|
|
|
Thread synchronization is a big topic, have a look at MSDN [^].
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Alright! Thanks for the link Thats exactly what i need.
|
|
|
|
|
You could create an array of 8 threads 0..7, iterate in a loop and have the threads read a line each.
Remember to lock the critical section when a thread accesses line.
While (count<10)
{
myThread[threadCount].readLineInto(myLineArray[count]);
threadCount++;if(threadCount>7) threadCount=0;
count++;
}
CCriticalSection cs;
cs.Lock();
cs.Unlock();
....
cs.Lock();
cs.Unlock();
Synchronized multi-threading in C++ (No MFC!)
Synchronized multi-threading in C++ (No MFC!)
class MyThread : public Thread {
private:
int m_nCount;
public:
MyThread(int n,const char* nm) {
Thread::setName(nm);
m_nCount = n;
}
void run() {
for(int i=0;i<m_nCount;i++) {
cout << getName().c_str() << ":" << i << endl;
}
}
};
int main() {
Thread *t1 = new MyThread(15,"Thread 01");
Thread *t2 = new MyThread(10,"Thread 02");
try {
t1->start();
t2->start();
t1->stop();
t2->stop();
}catch(ThreadException ex) {
printf("%s\n",ex.getMessage().c_str());
}
delete t1;
delete t2;
return 0;
|
|
|
|
|
csrss wrote: So how can i implement this? How to check if some line has been already read by some thread and another thread will not read it again?
Critical Section and a User defined DataStructure is needed here
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
I'm sorry, but that's a VERY crude example to learn multi-threading. Please do some reading on this subject (there are plenty of good books). Here's a fantastic link: http://www.flounder.com/workerthreads.htm[^] to begin with.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Bah ... ugly!
A file is a randomly accessible sequence of bytes, not "lines", since "lines" cannot be defined independently each other.
To give each thread a line to read, you must know where each line begins, and to know that you've to read all the preceding lines. At that point your "read" will be in any case sequential.
If you want to play this kind of game, try with a more structured file, containing "units" of fixed length.
---
Ah ... an opened file have just one "position", so it cannot be read simultaneously from different places. You need to open it more times (so that you can have independent "positions"), in read-sharing mode.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
Never, never access the same data between the threads.
Follow the small set of simple rules of threading.
The rules:
Rule #1: Do not access the same data between the threads.
Rule #2: Do not pass references or pointers to variables between threads.
Rule #3: Use windows messages to interact with the GUI.
#1 Do not access the same data between the threads.
The code passes the dialog's this pointer to the thread, but accessing isn't thread safe.
#2 Do not pass references or pointers to variables.
The code passes a pointer to the local ThreadParam variable.
If OnSomeButton goes out of scope before MyThreadProc tries to access the parameter
things will of course go bad.
Allocate with new and let the thread delete it when finished.
#3 Use windows messages to interact with the GUI.
|
|
|
|
|
Sorry for the late answer but ...
I've heard of the "rules" you're talking about relatively to MFC, because of its internal structure(and may relate to Wx having a similar structure), but the OP has never mentioned MFC.
If you work with HANDLE (for either files and threads) none of those rules necessarily applies.
Threads accessing same data are normal (and that's what synchronization objects like mutex or criticalsection are for ...)
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
How could I route all the windows messages of a dialog to a thread with their parameters?
|
|
|
|
|
|
Your Dialog messages will probably be filtered by you main message loop. Instead of letting the system handle these, you could:
1) Filter them yourself, and pass them as structured messages to your thread through some shared object, before letting the default handlers loose.
2) filter them yourself, and send them to a control in your in your other thread, again before letting the default handlers loose.
|
|
|
|
|
Thank you, But would you mind post a sample code or at least a link of some examples
regards
|
|
|
|
|
Are you using a framework (like MFC), or is this plain C/C++ ? i.e. who controls the message loop?
You'll find some plain examples here[^] (not exactly you problem, but close enough). In general, Raymond Chen's blog contains lots of interesting articles on message handling in Windows. ( After all, he wrote the bloody thing). You'll find that, if you're doing this in MFC, MFC only provides a thin layer on top of the standard functions, so it should be easy to reproduce the examples in MFC.
|
|
|
|
|
Yes I am using MFC, just a sample code or help may solve my problem
|
|
|
|
|
The API functions ::PostMessage and ::SendMessage are thread safe!
That means you can use the windows handle (ie the CWnd::m_hWn d) to interact with your MFC classes.
#define MYMESS_CALL_SOME_METHOD (WM_APP + 1)
struct ThreadParam
{
HWND mDlg;
};
UINT MyThreadProc( LPVOID pParam )
{
ThreadParam* p = static_cast<ThreadParam*> (pParam);
::SendMessage(p->mDlg, MYMESS_CALL_SOME_METHOD, 0, 0);
delete p;
}
void CMyDialog::OnSomeButton()
{
ThreadParam* param = new ThreadParam;
param->mDlg = m_hWnd;
AfxBeginThread(MyThreadProc, param);
param = 0;
}
Declare a method in the dialog message map (in the .h file)
...
afx_msg LRESULT OnCallSomeMethod(WPARAM, LPARAM);
DECLARE_MESSAGE_MAP()
Map the message to the method and implement the method (in the .cpp file):
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
...
ON_MESSAGE(MYMESS_CALL_SOME_METHOD, OnCallSomeMethod)
END_MESSAGE_MAP()
LRESULT CMyDialog::OnCallSomeMethod(WPARAM wp, LPARAM lp)
{
callSomeMethod();
return 0;
}
|
|
|
|
|
Michael Jansson wrote: ::SendMessage are thread safe!
Hi Michael,
Could you explain why SendMessage is thread-safe?
Best Wishes,
-David Delaune
|
|
|
|