Messagebox bestätigen / Threads?
-
Hi,
ich hab ein Programm gebastelt, allerdings bleibt es solange stehen bis man die Messagebox mit [OK] bestätigt hat, gibts hier Abhilfe oder einen Befehl womit man nur einen Befehl als Thread ausführen kann, so das nebenbei noch was gemacht werden kann? Ich wollte nicht für jede kleine Zeile einen Thread starten...
Habt ihr da eine Idee? Würd mir sehr weiterhelfen.Danke im Voraus
Gruß
-
void FUNKTION() { MessageBox("lol"); } main() { ...mache was... CreateThread(...FUNKTION...); ...mache was... ... ... }
so in etwa könnte dein konzept aussehen
-
Yepp, die Standard-Windows-Messagebox ist defaultmäßig modal.
Schau mal in der SDK, ob Du den Typ der Messagebox nach non-modal ändern kannst.
Ansonsten bastel Dir halt einen eigenen kleinen Dialog, der non-modal angezeigt wird. Aber ich bin mir sicher da gibt es schon was fertiges...
-
Ich zitier mal kurz:
MB_APPLMODAL
The user must respond to the message box before continuing work in the window identified by the hWnd parameter. However, the user can move to the windows of other threads and work in those windows.
Depending on the hierarchy of windows in the application, the user may be able to move to other windows within the thread. All child windows of the parent of the message box are automatically disabled, but pop-up windows are not.MB_APPLMODAL is the default if neither MB_SYSTEMMODAL nor MB_TASKMODAL is specified.
MB_SYSTEMMODAL
Same as MB_APPLMODAL except that the message box has the WS_EX_TOPMOST style. Use system-modal message boxes to notify the user of serious, potentially damaging errors that require immediate attention (for example, running out of memory). This flag has no effect on the user's ability to interact with windows other than those associated with hWnd.
MB_TASKMODAL
Same as MB_APPLMODAL except that all the top-level windows belonging to the current thread are disabled if the hWnd parameter is NULL. Use this flag when the calling application or library does not have a window handle available but still needs to prevent input to other windows in the calling thread without suspending other threads.
To specify other options, use one or more of the following values.Aja... so sieht eine Threadfunktion normal nicht aus... UINT_PTR oder UINT war der return Type... weiß es gerade net...
-
Babelduos Geist schrieb:
void FUNKTION() { MessageBox("lol"); } main() { ...mache was... CreateThread(...FUNKTION...); ...mache was... ... ... }
so in etwa könnte dein konzept aussehen
mal als lauffähiges beispiel:
#include <windows.h> #include <stdio.h> DWORD __stdcall thread (void *p) { return MessageBox (NULL, "hello", "msgbox", MB_OK); } int main() { DWORD tid; HANDLE h = CreateThread (NULL, 0, thread, 0, 0, &tid); while (WaitForSingleObject (h, 200) != WAIT_OBJECT_0) printf ("tick...\n"); printf ("MSGBOX clicked\n"); }
-
Hi,
danke für eure Antworten, ich hab da aber noch eine Frage.Ich habe jetzt diese Funktion geschrieben:
DWORD ShowMessageBox( const std::string& strText, const std::string& strTitle, UINT uType, HWND hWnd ) { return MessageBox( hWnd, strText.c_str(), strTitle.c_str(), uType ); }
Aber wie spreche ich in CreateThread die Parameter an?
CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ShowMessageBox, ?Parameter?, 0, &dwThreadID );
So gehts nicht. Wie ist es richtig bzw. geht das überhaupt, ansonsten hätte ich ein Problem?
Wäre prima, wenn ihr mir auch hier noch helfen könntet
Gruß
-
du kannst einen void* übergeben, somit müsstest du alles in eine struktur packen, zeiger übergeben und zurück casten.
bsp: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/creating_threads.asp[edit]
[vermutung]mit std::string könntest du probleme mit der threadsicherheit haben, nimm lieber mal nen char array. Wäre nett wenn mich jemand darüber aufklären könnte ob dies der fall ist oder nicht[/vermutung]
-
Danke, ich hab das nun alles so gemacht und funktioniert, aber eine Frage hab ich noch:
char *cPara; cPara = new char[strParameter.length() + 1]; strcpy(cPara, strParameter.c_str()); MessageBoxData *Data = new MessageBoxData(); Data->hWnd = hWnd; Data->strText = cPara; Data->strTitle = "Message"; Data->uType = MB_APPLMODAL; DWORD dwThreadID; HANDLE hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ShowMessageBox, Data, 0, &dwThreadID ); CloseHandle( hThread ); /* delete cPara; // Geht ja hier nicht zu löschen delete Data; // dito. */
Wo lösche ich den Speicher? Ich kann ihn hier ja schlecht löschen!
Danke im Voraus
Gruß
-
Warum willst du ein konzeptionelles Problem mit so einem Workaround wieder korrigieren?
Oder auf anders: Warum läßt du eine Messagebox anzeigen, wenn es vom Konzept nicht sinnvoll ist? Eine Messagebox benutzt man nun mal dann, wenn der Benutzer über etwas informiert werden soll oder etwas entscheiden muss bevor es im Programmablauf weitergehen kann. Da dies bei dir ja anscheinend nicht der Fall ist, it eine Messagebox fehl am Platz.
-
nachdem du deinen thread beendet hast kannst du doch den speicher löschen.
wieso legst du den speicher nicht auf dem stack an? dann übergibst du einfach einen zeiger darauf und beim verlassen des scopes wird alles automatisch zerstört.
MessageBoxData Data; Data.hWnd = hWnd; Data.strText = "irgendwas; // gehe davon aus das es hierbei um std::string handelt Data.strTitle = "Message"; // dito Data.uType = MB_APPLMODAL; HANDLE hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ShowMessageBox, (void*)&Data, 0, &dwThreadID );
-
miller_m schrieb:
...und beim verlassen des scopes wird alles automatisch zerstört.
dann musste aber dafür sorgen, dass der thread vorher beendet wurde...