Solved

DLL with UI Threads

Posted on 2001-07-16
5
163 Views
Last Modified: 2013-11-20
I have a CTreeCtrl and a List Box in the DLL.  When implementing the threads, the CTreeCtrl and List Box were destroyed when the control moves back to the main program.
How can I hold the CTreeCtrl and List Box (with data in them)?  Can anyone help?  Thanks.

0
Comment
Question by:cindywan
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
MichaelS earned 50 total points
ID: 6288851
You have to define it like global variables for example.
0
 

Author Comment

by:cindywan
ID: 6288866
I did.  I also use TldSetValue() to set (LPVOID) lParam into memory space, but it doesn't seems to work.
0
 
LVL 6

Expert Comment

by:MichaelS
ID: 6288873
Well, why than you accepted my comment as answer? Anyway, can you show your code?
0
 

Author Comment

by:cindywan
ID: 6292697
// TreeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RouteDLL2.h"
#include "TreeDlg.h"
#include <string.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


CString *Arrayx, *tmpRoute;
TVINSERTSTRUCT tvInsert;
HTREEITEM hRoot;
HTREEITEM hItem1, hItem2;
HTREEITEM hParent1, hParent2;
long IdxCnt1 = 0, numNode1 = 0, numNode2 = 0;

DWORD      dwTlsIndex1=0;
DWORD      dwTlsIndex2=0;

/////////////////////////////////////////////////////////////////////////////
// CTreeDlg dialog


CTreeDlg::CTreeDlg(CWnd* pParent /*=NULL*/)
      : CDialog(CTreeDlg::IDD, pParent)
{
      //{{AFX_DATA_INIT(CTreeDlg)
            // NOTE: the ClassWizard will add member initialization here
      //}}AFX_DATA_INIT
}


void CTreeDlg::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CTreeDlg)
      DDX_Control(pDX, IDC_BUTTON1, m_pButton1);
      DDX_Control(pDX, IDC_LIST3, m_pList3);
      DDX_Control(pDX, IDC_LIST2, m_pList2);
      DDX_Control(pDX, IDC_LIST1, m_pList1);
      DDX_Control(pDX, IDC_TREE2, m_pTree2);
      DDX_Control(pDX, IDC_TREE1, m_pTree1);
      //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTreeDlg, CDialog)
      //{{AFX_MSG_MAP(CTreeDlg)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTreeDlg message handlers

BOOL CTreeDlg::OnInitDialog()
{
      CDialog::OnInitDialog();

      return TRUE;  // return TRUE unless you set the focus to a control
                    // EXCEPTION: OCX Property Pages should return FALSE
}

void CTreeDlg::OnOK()
{
      CDialog::OnOK();
}


long CTreeDlg::StartRoute(long start, long end)
{
      long total;

      ThreadParams * th1 = new ThreadParams;
      ThreadParams * th2 = new ThreadParams;

      // Draw nodes on Tree 1
      tvInsert.hParent = NULL;
      tvInsert.hInsertAfter = NULL;
      tvInsert.item.mask = TVIF_TEXT;
      tvInsert.item.pszText = "Parent";
      hRoot = m_pTree1.InsertItem(&tvInsert);
      m_pTree1.EnsureVisible(hRoot);
                  
      for (IdxCnt1 = 0; IdxCnt1 < 5; IdxCnt1++)
      {
            tvInsert.hParent = hRoot;
            tvInsert.hInsertAfter = NULL;
            tvInsert.item.mask = TVIF_TEXT;
            tvInsert.item.pszText = "Child";
            HTREEITEM hFirst = m_pTree1.InsertItem(&tvInsert);
            if (IdxCnt1 == 0)      hParent1 = hFirst;
            m_pTree1.EnsureVisible(hFirst);
            m_pList1.InsertString(-1, "Child");
            m_pList1.SetCurSel(m_pList1.GetCount()-1);
      }

      // Draw nodes on Tree 2
      tvInsert.hParent = NULL;
      tvInsert.hInsertAfter = NULL;
      tvInsert.item.mask = TVIF_PARAM | TVIF_TEXT;
      tvInsert.item.pszText = "Parent";
      hRoot = m_pTree2.InsertItem(&tvInsert);
      m_pTree2.EnsureVisible(hRoot);
                  
      for (IdxCnt1 = 0; IdxCnt1 < 5; IdxCnt1++)
      {
            tvInsert.hParent = hRoot;
            tvInsert.hInsertAfter = NULL;
            tvInsert.item.mask = TVIF_PARAM | TVIF_TEXT;
            tvInsert.item.pszText = "Child";
            HTREEITEM hSecond = m_pTree2.InsertItem(&tvInsert);
            if (IdxCnt1 == 0)      hParent2 = hSecond;
            m_pTree2.EnsureVisible(hSecond);
            m_pList2.InsertString(-1, "Child");
            m_pList2.SetCurSel(m_pList2.GetCount()-1);
      }

      // threads start
      th1->list1 = &m_pList1;
      th1->list2 = &m_pList2;
      th1->list3 = &m_pList3;
      th1->tree1 = &m_pTree1;
      th1->tree2 = &m_pTree2;
      th1->cs = &mc;
      dwTlsIndex1 = TlsAlloc();
      AfxBeginThread(SubStart, (LPVOID) th1);
      Sleep(2000);
      
      th2->list1 = &m_pList1;
      th2->list2 = &m_pList2;
      th2->list3 = &m_pList3;
      th2->tree1 = &m_pTree1;
      th2->tree2 = &m_pTree2;
      th2->cs = &mc;
      dwTlsIndex2 = TlsAlloc();
      AfxBeginThread(SubEnd, (LPVOID) th2);
      Sleep(2000);

      return total = m_pList1.GetCount();
}


UINT SubStart(LPVOID pParam1)
{
      long s1 = 0, s2 = 0;


      ThreadParams *th1 = (ThreadParams *) pParam1;
      TlsSetValue(dwTlsIndex1, &th1);

      numNode1 = th1->tree1->GetCount();

      if (numNode1 == 0)
      {
            TlsFree(dwTlsIndex1);
            AfxEndThread(WM_QUIT, true);
            return true;
      }

      // Go through each child and add in two children each.
      for (s1 = 0; s1 < numNode1; s1++)
      {
            // higlight the next node
            th1->tree1->SelectItem(th1->tree1->GetNextItem(hParent1, TVGN_NEXT));
            hItem1 = th1->tree1->GetSelectedItem();

            // Add Children
            for (s2 = 0; s2 < 2; s2++)
            {
                  tvInsert.hParent = hParent1;
                  tvInsert.hInsertAfter = hParent1;
                  tvInsert.item.mask = TVIF_PARAM | TVIF_TEXT;
                  tvInsert.item.pszText = "GrandChild";
                  HTREEITEM hFirst = th1->tree1->InsertItem(&tvInsert);
                  th1->tree1->EnsureVisible(hFirst);
                  // Insert into a list box
                  th1->list1->InsertString(-1, "GrandChild");
                  th1->list1->SetCurSel(th1->list1->GetCount() - 1);
            }

            // if there is no next node in the tree
            if (hItem1 == NULL)
            {
                  TlsFree(dwTlsIndex1);
                  AfxEndThread(WM_QUIT, true);
                  return true;
            }
            else
            {
                  hParent1 = hItem1;
            }
      }

      TlsFree(dwTlsIndex1);
      AfxEndThread(WM_QUIT, true);
      return true;
}


UINT SubEnd(LPVOID pParam2)
{
      long e1 = 0, e2 = 0;

      ThreadParams *th2 = (ThreadParams *) pParam2;
      TlsSetValue(dwTlsIndex2, &th2);

      numNode2 = th2->tree2->GetCount();

      if (numNode2 == 0)
      {
            TlsFree(dwTlsIndex2);
            AfxEndThread(WM_QUIT, true);
            return true;
      }

      // Go through each child and add in two children each.
      for (e1 = 0; e1 < 5; e1++)
      {
            // higlight the next node
            th2->tree2->SelectItem(th2->tree2->GetNextItem(hParent2, TVGN_NEXT));
            hItem2 = th2->tree2->GetSelectedItem();

            // Add Children
            for (e2 = 0; e2 < 2; e2++)
            {
                  tvInsert.hParent = hParent2;
                  tvInsert.hInsertAfter = hParent2;
                  tvInsert.item.mask = TVIF_PARAM | TVIF_TEXT;
                  tvInsert.item.pszText = "GrandChild";
                  HTREEITEM hSecond = th2->tree2->InsertItem(&tvInsert);
                  th2->tree2->EnsureVisible(hSecond);
                  // Insert into a list box
                  th2->list2->InsertString(-1, "GrandChild");
                  th2->list2->SetCurSel(th2->list2->GetCount() - 1);
            }

            // if there is no next node in the tree
            if (hItem2 == NULL)
            {
                  TlsFree(dwTlsIndex2);
                  AfxEndThread(WM_QUIT, true);
                  return true;
            }
            else
            {
                  hParent2 = hItem2;
            }
      }

      TlsFree(dwTlsIndex2);
      AfxEndThread(WM_QUIT, true);
      return true;
}
0
 

Author Comment

by:cindywan
ID: 6292708

long __stdcall OFFRoute(long start, long end)
{
      AFX_MANAGE_STATE(AfxGetStaticModuleState());

      long i = 0;
      CTreeDlg dlg;

      dlg.Create(IDD_TREEDLG, NULL);
      dlg.ShowWindow(SW_SHOW);

      i = dlg.StartRoute (start, end);
      return i;
}


// TreeDlg.cpp : implementation file
//
#include "stdafx.h"
#include "RouteDLL2.h"
#include "TreeDlg.h"
#include <string.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


CString *Arrayx, *tmpRoute;
TVINSERTSTRUCT tvInsert;
HTREEITEM hRoot;
HTREEITEM hItem1, hItem2;
HTREEITEM hParent1, hParent2;
long IdxCnt1 = 0, numNode1 = 0, numNode2 = 0;

DWORD      dwTlsIndex1=0;
DWORD      dwTlsIndex2=0;

/////////////////////////////////////////////////////////////////////////////
// CTreeDlg dialog


CTreeDlg::CTreeDlg(CWnd* pParent /*=NULL*/)
      : CDialog(CTreeDlg::IDD, pParent)
{
      //{{AFX_DATA_INIT(CTreeDlg)
            // NOTE: the ClassWizard will add member initialization here
      //}}AFX_DATA_INIT
}


void CTreeDlg::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CTreeDlg)
      DDX_Control(pDX, IDC_BUTTON1, m_pButton1);
      DDX_Control(pDX, IDC_LIST3, m_pList3);
      DDX_Control(pDX, IDC_LIST2, m_pList2);
      DDX_Control(pDX, IDC_LIST1, m_pList1);
      DDX_Control(pDX, IDC_TREE2, m_pTree2);
      DDX_Control(pDX, IDC_TREE1, m_pTree1);
      //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTreeDlg, CDialog)
      //{{AFX_MSG_MAP(CTreeDlg)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTreeDlg message handlers

BOOL CTreeDlg::OnInitDialog()
{
      CDialog::OnInitDialog();

      return TRUE;  // return TRUE unless you set the focus to a control
                    // EXCEPTION: OCX Property Pages should return FALSE
}

void CTreeDlg::OnOK()
{
      CDialog::OnOK();
}


long CTreeDlg::StartRoute(long start, long end)
{
      long total;

      ThreadParams * th1 = new ThreadParams;
      ThreadParams * th2 = new ThreadParams;

      // Draw nodes on Tree 1
      tvInsert.hParent = NULL;
      tvInsert.hInsertAfter = NULL;
      tvInsert.item.mask = TVIF_TEXT;
      tvInsert.item.pszText = "Parent";
      hRoot = m_pTree1.InsertItem(&tvInsert);
      m_pTree1.EnsureVisible(hRoot);
                  
      for (IdxCnt1 = 0; IdxCnt1 < 5; IdxCnt1++)
      {
            tvInsert.hParent = hRoot;
            tvInsert.hInsertAfter = NULL;
            tvInsert.item.mask = TVIF_TEXT;
            tvInsert.item.pszText = "Child";
            HTREEITEM hFirst = m_pTree1.InsertItem(&tvInsert);
            if (IdxCnt1 == 0)      hParent1 = hFirst;
            m_pTree1.EnsureVisible(hFirst);
            m_pList1.InsertString(-1, "Child");
            m_pList1.SetCurSel(m_pList1.GetCount()-1);
      }

      // Draw nodes on Tree 2
      tvInsert.hParent = NULL;
      tvInsert.hInsertAfter = NULL;
      tvInsert.item.mask = TVIF_PARAM | TVIF_TEXT;
      tvInsert.item.pszText = "Parent";
      hRoot = m_pTree2.InsertItem(&tvInsert);
      m_pTree2.EnsureVisible(hRoot);
                  
      for (IdxCnt1 = 0; IdxCnt1 < 5; IdxCnt1++)
      {
            tvInsert.hParent = hRoot;
            tvInsert.hInsertAfter = NULL;
            tvInsert.item.mask = TVIF_PARAM | TVIF_TEXT;
            tvInsert.item.pszText = "Child";
            HTREEITEM hSecond = m_pTree2.InsertItem(&tvInsert);
            if (IdxCnt1 == 0)      hParent2 = hSecond;
            m_pTree2.EnsureVisible(hSecond);
            m_pList2.InsertString(-1, "Child");
            m_pList2.SetCurSel(m_pList2.GetCount()-1);
      }

      // threads start
      th1->list1 = &m_pList1;
      th1->list2 = &m_pList2;
      th1->list3 = &m_pList3;
      th1->tree1 = &m_pTree1;
      th1->tree2 = &m_pTree2;
      th1->cs = &mc;
      dwTlsIndex1 = TlsAlloc();
      AfxBeginThread(SubStart, (LPVOID) th1);
      Sleep(2000);
      
      th2->list1 = &m_pList1;
      th2->list2 = &m_pList2;
      th2->list3 = &m_pList3;
      th2->tree1 = &m_pTree1;
      th2->tree2 = &m_pTree2;
      th2->cs = &mc;
      dwTlsIndex2 = TlsAlloc();
      AfxBeginThread(SubEnd, (LPVOID) th2);
      Sleep(2000);

      return total = m_pList1.GetCount();
}


UINT SubStart(LPVOID pParam1)
{
      long s1 = 0, s2 = 0;


      ThreadParams *th1 = (ThreadParams *) pParam1;
      TlsSetValue(dwTlsIndex1, &th1);

      numNode1 = th1->tree1->GetCount();

      if (numNode1 == 0)
      {
            TlsFree(dwTlsIndex1);
            AfxEndThread(WM_QUIT, true);
            return true;
      }

      // Go through each child and add in two children each.
      for (s1 = 0; s1 < numNode1; s1++)
      {
            // higlight the next node
            th1->tree1->SelectItem(th1->tree1->GetNextItem(hParent1, TVGN_NEXT));
            hItem1 = th1->tree1->GetSelectedItem();

            // Add Children
            for (s2 = 0; s2 < 2; s2++)
            {
                  tvInsert.hParent = hParent1;
                  tvInsert.hInsertAfter = hParent1;
                  tvInsert.item.mask = TVIF_PARAM | TVIF_TEXT;
                  tvInsert.item.pszText = "GrandChild";
                  HTREEITEM hFirst = th1->tree1->InsertItem(&tvInsert);
                  th1->tree1->EnsureVisible(hFirst);
                  // Insert into a list box
                  th1->list1->InsertString(-1, "GrandChild");
                  th1->list1->SetCurSel(th1->list1->GetCount() - 1);
            }

            // if there is no next node in the tree
            if (hItem1 == NULL)
            {
                  TlsFree(dwTlsIndex1);
                  AfxEndThread(WM_QUIT, true);
                  return true;
            }
            else
            {
                  hParent1 = hItem1;
            }
      }

      TlsFree(dwTlsIndex1);
      AfxEndThread(WM_QUIT, true);
      return true;
}


UINT SubEnd(LPVOID pParam2)
{
      long e1 = 0, e2 = 0;

      ThreadParams *th2 = (ThreadParams *) pParam2;
      TlsSetValue(dwTlsIndex2, &th2);

      numNode2 = th2->tree2->GetCount();

      if (numNode2 == 0)
      {
            TlsFree(dwTlsIndex2);
            AfxEndThread(WM_QUIT, true);
            return true;
      }

      // Go through each child and add in two children each.
      for (e1 = 0; e1 < 5; e1++)
      {
            // higlight the next node
            th2->tree2->SelectItem(th2->tree2->GetNextItem(hParent2, TVGN_NEXT));
            hItem2 = th2->tree2->GetSelectedItem();

            // Add Children
            for (e2 = 0; e2 < 2; e2++)
            {
                  tvInsert.hParent = hParent2;
                  tvInsert.hInsertAfter = hParent2;
                  tvInsert.item.mask = TVIF_PARAM | TVIF_TEXT;
                  tvInsert.item.pszText = "GrandChild";
                  HTREEITEM hSecond = th2->tree2->InsertItem(&tvInsert);
                  th2->tree2->EnsureVisible(hSecond);
                  // Insert into a list box
                  th2->list2->InsertString(-1, "GrandChild");
                  th2->list2->SetCurSel(th2->list2->GetCount() - 1);
            }

            // if there is no next node in the tree
            if (hItem2 == NULL)
            {
                  TlsFree(dwTlsIndex2);
                  AfxEndThread(WM_QUIT, true);
                  return true;
            }
            else
            {
                  hParent2 = hItem2;
            }
      }

      TlsFree(dwTlsIndex2);
      AfxEndThread(WM_QUIT, true);
      return true;
}

//////////////////
// Test Program //
//////////////////
case IDC_BUTTON1:
{
      
    total = OFFRoute(1, 10);
    if (total >= 0)
      MessageBox(NULL, "Result found", NULL, MB_OK);
    else
      MessageBox(NULL, "No Result found", NULL, MB_OK);
    break;
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Folder Comparison 12 53
List out all word 7 260
dog bark java program 15 87
Modbus - whats the maximum I can store in one register? 4 78
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now