Advertisement
Guest User

Głupi Sadol

a guest
Nov 24th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.84 KB | None | 0 0
  1. // Okno.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "lab3.h"
  6. #include "Okno.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #endif
  11.  
  12. int volatile licznik;
  13. bool volatile przerwij;
  14. int czas_pracy[5];
  15. char wynik[255];
  16. bool jest_wynik;
  17. CCriticalSection ekran;
  18. CEvent odebrano;
  19.  
  20. #define WM_WIADOMOSC (WM_USER+100)
  21.  
  22. // CAboutDlg dialog used for App About
  23.  
  24. class CAboutDlg : public CDialog
  25. {
  26. public:
  27.     CAboutDlg();
  28.  
  29. // Dialog Data
  30.     enum { IDD = IDD_ABOUTBOX };
  31.  
  32.     protected:
  33.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  34.  
  35. // Implementation
  36. protected:
  37.     DECLARE_MESSAGE_MAP()
  38. };
  39.  
  40. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  41. {
  42. }
  43.  
  44. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  45. {
  46.     CDialog::DoDataExchange(pDX);
  47. }
  48.  
  49. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  50. END_MESSAGE_MAP()
  51.  
  52. // COkno dialog
  53.  
  54.  
  55. COkno::COkno(CWnd* pParent /*=NULL*/)
  56.     : CDialog(COkno::IDD, pParent)
  57. {
  58.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  59. }
  60.  
  61. void COkno::DoDataExchange(CDataExchange* pDX)
  62. {
  63.     CDialog::DoDataExchange(pDX);
  64.     DDX_Control(pDX, IDC_EKRAN, m_ekran);
  65.     DDX_Control(pDX, IDC_W1, m_w1);
  66.     DDX_Control(pDX, IDC_W2, m_w2);
  67.     DDX_Control(pDX, IDC_W3, m_w3);
  68.     DDX_Control(pDX, IDC_W4, m_w4);
  69.     DDX_Control(pDX, IDC_W5, m_w5);
  70.     DDX_Control(pDX, IDC_URUCHOM, m_uruchom);
  71.     DDX_Control(pDX, IDC_PRZERWIJ, m_przerwij);
  72.     DDX_Control(pDX, IDC_KONIEC, m_koniec);
  73. }
  74.  
  75. BEGIN_MESSAGE_MAP(COkno, CDialog)
  76.     ON_WM_SYSCOMMAND()
  77.     ON_WM_PAINT()
  78.     ON_WM_QUERYDRAGICON()
  79.     //}}AFX_MSG_MAP
  80.     ON_MESSAGE(WM_WIADOMOSC, OnWiadomosc)
  81.     ON_BN_CLICKED(IDC_URUCHOM, OnBnClickedUruchom)
  82.     ON_WM_TIMER()
  83.     ON_BN_CLICKED(IDC_PRZERWIJ, OnBnClickedPrzerwij)
  84.     ON_BN_CLICKED(IDC_KONIEC, OnBnClickedKoniec)
  85.     ON_WM_DESTROY()
  86. END_MESSAGE_MAP()
  87.  
  88. // COkno message handlers
  89.  
  90. BOOL COkno::OnInitDialog()
  91. {
  92.     CDialog::OnInitDialog();
  93.  
  94.     // Add "About..." menu item to system menu.
  95.  
  96.     // IDM_ABOUTBOX must be in the system command range.
  97.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  98.     ASSERT(IDM_ABOUTBOX < 0xF000);
  99.  
  100.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  101.     if (pSysMenu != NULL)
  102.     {
  103.         CString strAboutMenu;
  104.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  105.         if (!strAboutMenu.IsEmpty())
  106.         {
  107.             pSysMenu->AppendMenu(MF_SEPARATOR);
  108.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  109.         }
  110.     }
  111.  
  112.     // Set the icon for this dialog.  The framework does this automatically
  113.     //  when the application's main window is not a dialog
  114.     SetIcon(m_hIcon, TRUE);            // Set big icon
  115.     SetIcon(m_hIcon, FALSE);        // Set small icon
  116.  
  117.     // Add extra initialization here
  118.     srand((unsigned )time(0));
  119.     SetTimer(1,4000,0);
  120.  
  121.    
  122.     return TRUE;  // return TRUE  unless you set the focus to a control
  123. }
  124.  
  125. void COkno::OnSysCommand(UINT nID, LPARAM lParam)
  126. {
  127.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  128.     {
  129.         CAboutDlg dlgAbout;
  130.         dlgAbout.DoModal();
  131.     }
  132.     else
  133.     {
  134.         CDialog::OnSysCommand(nID, lParam);
  135.     }
  136. }
  137.  
  138. // If you add a minimize button to your dialog, you will need the code below
  139. //  to draw the icon.  For MFC applications using the document/view model,
  140. //  this is automatically done for you by the framework.
  141.  
  142. void COkno::OnPaint()
  143. {
  144.     if (IsIconic())
  145.     {
  146.         CPaintDC dc(this); // device context for painting
  147.  
  148.         SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  149.  
  150.         // Center icon in client rectangle
  151.         int cxIcon = GetSystemMetrics(SM_CXICON);
  152.         int cyIcon = GetSystemMetrics(SM_CYICON);
  153.         CRect rect;
  154.         GetClientRect(&rect);
  155.         int x = (rect.Width() - cxIcon + 1) / 2;
  156.         int y = (rect.Height() - cyIcon + 1) / 2;
  157.  
  158.         // Draw the icon
  159.         dc.DrawIcon(x, y, m_hIcon);
  160.     }
  161.     else
  162.     {
  163.         CDialog::OnPaint();
  164.     }
  165. }
  166.  
  167. // The system calls this function to obtain the cursor to display while the user drags
  168. //  the minimized window.
  169. HCURSOR COkno::OnQueryDragIcon()
  170. {
  171.     return static_cast<HCURSOR>(m_hIcon);
  172. }
  173.  
  174. UINT COkno::WatekRoboczy(void * pParametr)
  175. {
  176.  
  177.     licznik++;
  178.     int i = (int )pParametr;
  179.  
  180.     do {
  181.         if(przerwij==true)
  182.         {
  183.             AfxGetMainWnd()->PostMessage(WM_WIADOMOSC, i, 4);
  184.             return 0;
  185.         }
  186.         AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,1);
  187.         Sleep(500);
  188.         if(czas_pracy[i]<=0) break;
  189.         czas_pracy[i]--;
  190.     } while(1);
  191.  
  192.     AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,2);
  193.     ekran.Lock();
  194.     sprintf(wynik, "Rozwiazanie z %d watku\r\n", i+1);
  195.     jest_wynik = true;
  196.  
  197.     AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,3);
  198.     CSingleLock sl(&odebrano);
  199.     sl.Lock();
  200.     ekran.Unlock();
  201.    
  202.     AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,4);
  203.     licznik--;
  204.  
  205.     return 0;
  206. }
  207.  
  208. void COkno::OnBnClickedUruchom()
  209. {
  210.     m_uruchom.EnableWindow(FALSE);
  211.     m_przerwij.EnableWindow();
  212.  
  213.     przerwij = false;
  214.     for(int i=0; i<5; i++)
  215.     {
  216.         czas_pracy[i] = 10+(30*rand())/RAND_MAX;
  217.         AfxBeginThread(WatekRoboczy, (void *)i);
  218.     }
  219.  
  220. }
  221.  
  222. afx_msg LRESULT COkno::OnWiadomosc(WPARAM n, LPARAM m)
  223. {
  224.         static char* w[4] =
  225.     {"Oblicza...", "Czeka na ekran...", "Wyswietla wynik...", "Zakonczyl prace"};  
  226.  
  227.     CString tmp;    
  228.     tmp = w[m-1];
  229.  
  230. //TODO: Dodatkowa obsluga wyswietlania pozostalego czasu obliczen
  231.  
  232.    
  233.     switch (n) {
  234.     case 0: m_w1.SetWindowText(tmp);
  235.         break;
  236.     case 1: m_w2.SetWindowText(tmp);
  237.         break;
  238.     case 2: m_w3.SetWindowText(tmp);
  239.         break;
  240.     case 3: m_w4.SetWindowText(tmp);
  241.         break;
  242.     case 4: m_w5.SetWindowText(tmp);
  243.         break;
  244.     }
  245.  
  246.     return 0;
  247. }
  248.  
  249. void COkno::OnTimer(UINT nIDEvent)
  250. {
  251.     CString s;    
  252.     char buffer[1024];
  253.    
  254.     CDialog::OnTimer(nIDEvent);
  255.  
  256.     if(jest_wynik != true)
  257.     {
  258.         return;
  259.     }
  260.     m_ekran.GetWindowText(s);
  261.     s=s+wynik;
  262.     m_ekran.SetWindowText(s);
  263.     jest_wynik=false;
  264.     odebrano.SetEvent();
  265.  
  266.  
  267.     //m_ekran.GetWindowText(buffer,1024);
  268.     //s = buffer;
  269.     /*TODO: dodanie zmiennej z wynikiem do s */;
  270.  
  271.     //m_ekran.SetWindowText(s);
  272.    
  273.     /*TODO: Ustawienie stanu odpowiedniego obiektu synchronizacji*/
  274.  
  275. }
  276.  
  277. void COkno::OnBnClickedPrzerwij()
  278. {
  279.     /*TODO: Ustawienie zmiennej informujacej o wcisnieciu "Przerwij"*/
  280.  
  281.     przerwij = true;
  282.     m_uruchom.EnableWindow();
  283.     m_przerwij.EnableWindow(FALSE);
  284.     OnTimer(1);
  285.     while(licznik>0)
  286.     {
  287.          Sleep(500);
  288.          licznik--;
  289.          
  290.     }
  291.     ekran.Unlock();
  292.    
  293.     /*TODO: Zaczekac na koniec wszystkich watkow*/
  294. }
  295.  
  296. void COkno::OnBnClickedKoniec()
  297. {
  298.     EndDialog(1);
  299. }
  300.  
  301. void COkno::OnDestroy()
  302. {
  303.     KillTimer(1);
  304.     OnBnClickedPrzerwij();
  305.     CDialog::OnDestroy();
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement