Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with the ListView control [modified]

Problem with the ListView control [modified]

Scheduled Pinned Locked Moved C#
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ankit Rajpoot
    wrote on last edited by
    #1

    Hello everybody, My application uses a ListView control to display a list of files. Everything works fine except that when I set the View property of the ListView to View.List, the list appears but, here's[^] a screenshot of how it looks. I can't understand, why the filenames are condensed and appended with a "...". I've searched google and msdn, but couldn't found a solution for the problem. The Large Icon[^] and Details Views[^] are working perfectly. Just the list view is misbehaving. My second problem is regarding a BackgroundWorker. I have a BGWKR that I'm using to perform a database transaction. Now the problem is that the user may close the form while the transaction is in progress. To handle this, I added code to the form's closing event handler, to inform the user that a transaction is active and ask him/her, whether to cancel it. If the user affirms, then a call to BGWKR.CancelAsync() is made. Here's the form_closing event.

    private void WndSearchResults_FormClosing(object sender, FormClosingEventArgs e)
    {
    if (bgDatabaseSearcher.IsBusy)
    {
    if (MessageBox.Show(Resources.msgSearchActive_WindowClosing, "Question",
    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
    {
    e.Cancel = true;
    return;
    }
    else
    {
    bgDatabaseSearcher.CancelAsync();
    while (bgDatabaseSearcher.IsBusy)
    Thread.Sleep(500);
    }
    }
    }

    Here's the BGWKR's Do_Work event

    private void bgDatabaseSearcher_DoWork(object sender, DoWorkEventArgs e)
    {
    VFS virtualFS = new VFS();

            foreach (ListEntry entry in imagesToSearch)
            {
                try
                {
                    virtualFS.OpenConnection(Path.Combine(AppSettingsManager.ImagesDirectory, entry.imageDbPath));
                    foreach (VFSSearchTerm searchTerm in termsToSearch)
                    {
    
    L 2 Replies Last reply
    0
    • A Ankit Rajpoot

      Hello everybody, My application uses a ListView control to display a list of files. Everything works fine except that when I set the View property of the ListView to View.List, the list appears but, here's[^] a screenshot of how it looks. I can't understand, why the filenames are condensed and appended with a "...". I've searched google and msdn, but couldn't found a solution for the problem. The Large Icon[^] and Details Views[^] are working perfectly. Just the list view is misbehaving. My second problem is regarding a BackgroundWorker. I have a BGWKR that I'm using to perform a database transaction. Now the problem is that the user may close the form while the transaction is in progress. To handle this, I added code to the form's closing event handler, to inform the user that a transaction is active and ask him/her, whether to cancel it. If the user affirms, then a call to BGWKR.CancelAsync() is made. Here's the form_closing event.

      private void WndSearchResults_FormClosing(object sender, FormClosingEventArgs e)
      {
      if (bgDatabaseSearcher.IsBusy)
      {
      if (MessageBox.Show(Resources.msgSearchActive_WindowClosing, "Question",
      MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
      {
      e.Cancel = true;
      return;
      }
      else
      {
      bgDatabaseSearcher.CancelAsync();
      while (bgDatabaseSearcher.IsBusy)
      Thread.Sleep(500);
      }
      }
      }

      Here's the BGWKR's Do_Work event

      private void bgDatabaseSearcher_DoWork(object sender, DoWorkEventArgs e)
      {
      VFS virtualFS = new VFS();

              foreach (ListEntry entry in imagesToSearch)
              {
                  try
                  {
                      virtualFS.OpenConnection(Path.Combine(AppSettingsManager.ImagesDirectory, entry.imageDbPath));
                      foreach (VFSSearchTerm searchTerm in termsToSearch)
                      {
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, 1. there seems to be no easy solution. This article[^] is probably the best approach, however I never tried it. 2. I did not understand the problem; also the post is too wide for easy reading. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      A 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, 1. there seems to be no easy solution. This article[^] is probably the best approach, however I never tried it. 2. I did not understand the problem; also the post is too wide for easy reading. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


        A Offline
        A Offline
        Ankit Rajpoot
        wrote on last edited by
        #3

        My apologies for the post being too much wide. I've reformatted it now.

        Excuse me for buttin' in, but I'm interrupt driven.

        1 Reply Last reply
        0
        • A Ankit Rajpoot

          Hello everybody, My application uses a ListView control to display a list of files. Everything works fine except that when I set the View property of the ListView to View.List, the list appears but, here's[^] a screenshot of how it looks. I can't understand, why the filenames are condensed and appended with a "...". I've searched google and msdn, but couldn't found a solution for the problem. The Large Icon[^] and Details Views[^] are working perfectly. Just the list view is misbehaving. My second problem is regarding a BackgroundWorker. I have a BGWKR that I'm using to perform a database transaction. Now the problem is that the user may close the form while the transaction is in progress. To handle this, I added code to the form's closing event handler, to inform the user that a transaction is active and ask him/her, whether to cancel it. If the user affirms, then a call to BGWKR.CancelAsync() is made. Here's the form_closing event.

          private void WndSearchResults_FormClosing(object sender, FormClosingEventArgs e)
          {
          if (bgDatabaseSearcher.IsBusy)
          {
          if (MessageBox.Show(Resources.msgSearchActive_WindowClosing, "Question",
          MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
          {
          e.Cancel = true;
          return;
          }
          else
          {
          bgDatabaseSearcher.CancelAsync();
          while (bgDatabaseSearcher.IsBusy)
          Thread.Sleep(500);
          }
          }
          }

          Here's the BGWKR's Do_Work event

          private void bgDatabaseSearcher_DoWork(object sender, DoWorkEventArgs e)
          {
          VFS virtualFS = new VFS();

                  foreach (ListEntry entry in imagesToSearch)
                  {
                      try
                      {
                          virtualFS.OpenConnection(Path.Combine(AppSettingsManager.ImagesDirectory, entry.imageDbPath));
                          foreach (VFSSearchTerm searchTerm in termsToSearch)
                          {
          
          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, much better now. I still don't see what your problem #2 is, you say it works?? I do have 3 comments: 1. assuming lViewSResultsBrowser is a ListView, you are not allowed to perform a Groups.Add() operation (or any other Control operation) on it since Controls are not thread-safe. Without an InvokeRequired/Invoke construct it may hang your GUI temporarily or forever. 2. IMO all that is required for the cancellation is a simple if (bgDatabaseSearcher.CancellationPending) break; since that will bring you in the finally block, hence close the connection and return. 3. I don't particularly like the loop while (bgDatabaseSearcher.IsBusy) Thread.Sleep(500); since it makes your GUI hang for as long as the BGW takes; you have made your GUI thread a slave of your background worker now, so if the BGW never terminates, your app is stuck while the user wanted it to exit. The minimum I would do is impose an upper limit. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


          A 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, much better now. I still don't see what your problem #2 is, you say it works?? I do have 3 comments: 1. assuming lViewSResultsBrowser is a ListView, you are not allowed to perform a Groups.Add() operation (or any other Control operation) on it since Controls are not thread-safe. Without an InvokeRequired/Invoke construct it may hang your GUI temporarily or forever. 2. IMO all that is required for the cancellation is a simple if (bgDatabaseSearcher.CancellationPending) break; since that will bring you in the finally block, hence close the connection and return. 3. I don't particularly like the loop while (bgDatabaseSearcher.IsBusy) Thread.Sleep(500); since it makes your GUI hang for as long as the BGW takes; you have made your GUI thread a slave of your background worker now, so if the BGW never terminates, your app is stuck while the user wanted it to exit. The minimum I would do is impose an upper limit. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


            A Offline
            A Offline
            Ankit Rajpoot
            wrote on last edited by
            #5

            Hi, Thanks for your response. Well, my problem is not that the BGWKR is not working. Infact, it's working perfectly. My problem is that if I call CancelAsync() from FormClosing event, the Do_Work method never stops execution and the .IsBusy property never becomes false. At the same time, the code inside the Do_Work event seems to be jammed. I can say this as the if(CancellationPending) condition in the Do_Work event is never hit. The very same architecture works flawlessely, it CancelAsync() is called from a button's click event handler. Your 1st comment: That Groups.Add() call is nothing. I added it for testing purposes while debugging. So don't worry about that. I know it's not a thread-safe operation and will remove it. Your 2nd comment: Doing so will only get me out of the inner foreach loop. What about the outer one? Your 3rd comment: That i'll replace with code to hide the form and then wait till the BGWKR finishes or a deadline is met.

            Excuse me for buttin' in, but I'm interrupt driven.

            L 1 Reply Last reply
            0
            • A Ankit Rajpoot

              Hi, Thanks for your response. Well, my problem is not that the BGWKR is not working. Infact, it's working perfectly. My problem is that if I call CancelAsync() from FormClosing event, the Do_Work method never stops execution and the .IsBusy property never becomes false. At the same time, the code inside the Do_Work event seems to be jammed. I can say this as the if(CancellationPending) condition in the Do_Work event is never hit. The very same architecture works flawlessely, it CancelAsync() is called from a button's click event handler. Your 1st comment: That Groups.Add() call is nothing. I added it for testing purposes while debugging. So don't worry about that. I know it's not a thread-safe operation and will remove it. Your 2nd comment: Doing so will only get me out of the inner foreach loop. What about the outer one? Your 3rd comment: That i'll replace with code to hide the form and then wait till the BGWKR finishes or a deadline is met.

              Excuse me for buttin' in, but I'm interrupt driven.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              OK, here is an hypothesis: maybe CancelAsync does not get forwarded to the BGW (or has no net effect) as long as the GUI thread is still busy; when inside a Button_Click handler, it is free right away; in your Form_Closing handler, you keep the GUI thread occupied with the sleep loop. suggested experiment: rather than the while-sleep loop, set e.Handled true and see if the CancelAsync now works; if it does, it confirms the hypothesis; I then suggest you don't use CancelAsync, instead create your own boolean variable to communicate from Form_Closing to DoWork. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


              A 1 Reply Last reply
              0
              • L Luc Pattyn

                OK, here is an hypothesis: maybe CancelAsync does not get forwarded to the BGW (or has no net effect) as long as the GUI thread is still busy; when inside a Button_Click handler, it is free right away; in your Form_Closing handler, you keep the GUI thread occupied with the sleep loop. suggested experiment: rather than the while-sleep loop, set e.Handled true and see if the CancelAsync now works; if it does, it confirms the hypothesis; I then suggest you don't use CancelAsync, instead create your own boolean variable to communicate from Form_Closing to DoWork. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                A Offline
                A Offline
                Ankit Rajpoot
                wrote on last edited by
                #7

                Thanks, Your hypothesis was perfectly correct. When I removed the while(...)...; loop from the form_closing event, it started working perfectly.

                Excuse me for buttin' in, but I'm interrupt driven.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups