Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Okno.cpp : implementation file
- //
- #include "stdafx.h"
- #include "lab3.h"
- #include "Okno.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- int volatile licznik;
- bool volatile przerwij;
- int czas_pracy[5];
- char wynik[255];
- bool jest_wynik;
- CCriticalSection ekran;
- CEvent odebrano;
- #define WM_WIADOMOSC (WM_USER+100)
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- // Implementation
- protected:
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- END_MESSAGE_MAP()
- // COkno dialog
- COkno::COkno(CWnd* pParent /*=NULL*/)
- : CDialog(COkno::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void COkno::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Control(pDX, IDC_EKRAN, m_ekran);
- DDX_Control(pDX, IDC_W1, m_w1);
- DDX_Control(pDX, IDC_W2, m_w2);
- DDX_Control(pDX, IDC_W3, m_w3);
- DDX_Control(pDX, IDC_W4, m_w4);
- DDX_Control(pDX, IDC_W5, m_w5);
- DDX_Control(pDX, IDC_URUCHOM, m_uruchom);
- DDX_Control(pDX, IDC_PRZERWIJ, m_przerwij);
- DDX_Control(pDX, IDC_KONIEC, m_koniec);
- }
- BEGIN_MESSAGE_MAP(COkno, CDialog)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- ON_MESSAGE(WM_WIADOMOSC, OnWiadomosc)
- ON_BN_CLICKED(IDC_URUCHOM, OnBnClickedUruchom)
- ON_WM_TIMER()
- ON_BN_CLICKED(IDC_PRZERWIJ, OnBnClickedPrzerwij)
- ON_BN_CLICKED(IDC_KONIEC, OnBnClickedKoniec)
- ON_WM_DESTROY()
- END_MESSAGE_MAP()
- // COkno message handlers
- BOOL COkno::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // Add extra initialization here
- srand((unsigned )time(0));
- SetTimer(1,4000,0);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void COkno::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void COkno::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this function to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR COkno::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- UINT COkno::WatekRoboczy(void * pParametr)
- {
- licznik++;
- int i = (int )pParametr;
- do {
- if(przerwij==true)
- {
- AfxGetMainWnd()->PostMessage(WM_WIADOMOSC, i, 4);
- return 0;
- }
- AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,1);
- Sleep(500);
- if(czas_pracy[i]<=0) break;
- czas_pracy[i]--;
- } while(1);
- AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,2);
- ekran.Lock();
- sprintf(wynik, "Rozwiazanie z %d watku\r\n", i+1);
- jest_wynik = true;
- AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,3);
- CSingleLock sl(&odebrano);
- sl.Lock();
- ekran.Unlock();
- AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,4);
- licznik--;
- return 0;
- }
- void COkno::OnBnClickedUruchom()
- {
- m_uruchom.EnableWindow(FALSE);
- m_przerwij.EnableWindow();
- przerwij = false;
- for(int i=0; i<5; i++)
- {
- czas_pracy[i] = 10+(30*rand())/RAND_MAX;
- AfxBeginThread(WatekRoboczy, (void *)i);
- }
- }
- afx_msg LRESULT COkno::OnWiadomosc(WPARAM n, LPARAM m)
- {
- static char* w[4] =
- {"Oblicza...", "Czeka na ekran...", "Wyswietla wynik...", "Zakonczyl prace"};
- CString tmp;
- tmp = w[m-1];
- //TODO: Dodatkowa obsluga wyswietlania pozostalego czasu obliczen
- switch (n) {
- case 0: m_w1.SetWindowText(tmp);
- break;
- case 1: m_w2.SetWindowText(tmp);
- break;
- case 2: m_w3.SetWindowText(tmp);
- break;
- case 3: m_w4.SetWindowText(tmp);
- break;
- case 4: m_w5.SetWindowText(tmp);
- break;
- }
- return 0;
- }
- void COkno::OnTimer(UINT nIDEvent)
- {
- CString s;
- char buffer[1024];
- CDialog::OnTimer(nIDEvent);
- if(jest_wynik != true)
- {
- return;
- }
- m_ekran.GetWindowText(s);
- s=s+wynik;
- m_ekran.SetWindowText(s);
- jest_wynik=false;
- odebrano.SetEvent();
- //m_ekran.GetWindowText(buffer,1024);
- //s = buffer;
- /*TODO: dodanie zmiennej z wynikiem do s */;
- //m_ekran.SetWindowText(s);
- /*TODO: Ustawienie stanu odpowiedniego obiektu synchronizacji*/
- }
- void COkno::OnBnClickedPrzerwij()
- {
- /*TODO: Ustawienie zmiennej informujacej o wcisnieciu "Przerwij"*/
- przerwij = true;
- m_uruchom.EnableWindow();
- m_przerwij.EnableWindow(FALSE);
- OnTimer(1);
- while(licznik>0)
- {
- Sleep(500);
- licznik--;
- }
- ekran.Unlock();
- /*TODO: Zaczekac na koniec wszystkich watkow*/
- }
- void COkno::OnBnClickedKoniec()
- {
- EndDialog(1);
- }
- void COkno::OnDestroy()
- {
- KillTimer(1);
- OnBnClickedPrzerwij();
- CDialog::OnDestroy();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement