?
Solved

DLL with UI Threads

Posted on 2001-07-16
5
Medium Priority
?
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
MichaelS earned 100 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

719 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