CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2011
    Posts
    17

    MultiThreading Help need please

    Hi, Im just begining to learn multithreading and decided to add support for it in a program I previously built. It consists of 3 main tasks I separated into 3 methods that I wanted to launch on separate threads: GroupFiles(), MergeFiles(), ImportFile().

    The reason for adding support was to prevent my application from becoming unresponsive when it calls the methods but instead what happens is my application window is still unresponsive albeit it does not say "Not Responding" like it did before. In other words I cant move, resize, or click anything in the main window while the methods are executing, I cant even close the app by X'ing out. I have a button in the app that is supposed prematurely end the calculations but I cant even click that button.

    I suspect it might be because of improper use of the lock keyword. I wrapped all the logic in both GroupFiles() and MergeFiles() within a lock statement with the same lock token. I didnt lock down the code in ImportFile() method (which takes about an hour to process) because I wanted to lock down the tickCollection variable outside the method call and I wasnt sure if it would be smart to create new threads for ImportFile() on an already secondary thread (for the loop).

    What am I doing wrong please tell. I can post more code as needed

    Code:
    private void importButton_Click(object sender, RoutedEventArgs e)
            {
                
                statusText.Text = "Organizing files";
                Thread groupFilesThread = new Thread(new ThreadStart(GroupFiles));
                groupFilesThread.Name = "Group Files Thread";
                groupFilesThread.IsBackground = true;
                groupFilesThread.Start();
                groupFilesHandle.WaitOne();    //AutoResetEvent handle. Set() called from function
    
                statusText.Text = "Merging Files";
                Thread mergeFilesThread = new Thread(new ThreadStart(MergeFiles));
                mergeFilesThread.Name = "Merge Files Thread";
                mergeFilesThread.IsBackground = true;
                mergeFilesThread.Start();
                mergeFilesHandle.WaitOne(); 
    
    
                lock (lockObject)
                {
                    //Import tick files into RightEdge
                    foreach (string tickFile in tickFiles)
                    {
                        string stringSymbol = System.IO.Path.GetFileNameWithoutExtension(tickFile).Insert(3, "/");  //Change AUDJPY to AUD/JPY
                        Symbol symbol = new Symbol(stringSymbol);
                        symbol.AssetClass = AssetClass.*****;
                        statusText.Text = string.Format("Importing {0} into RightEdge", symbol.ToString());
    
                        FileParamInfo importInfo = new FileParamInfo(tickFile, symbol);  //Light weight helper class for ParameterizedThreadStart()
    
                        statusText.Text = string.Format("Importing tick file for {0}", stringSymbol);
                        Thread importThread = new Thread(new ParameterizedThreadStart(ImportFile));
                        importThread.Name = string.Format("{0} Import Thread", symbol.ToString());
                        importThread.Start(importInfo);
                        importFileswaitHandle.WaitOne();
                    } 
                }
    
                statusText.Text = "Done!";
                ConsoleManager.Hide();
                
            } //End importButton_Click
    
    
    
    private void GroupFiles()
            {
                lock (lockObject)
                { 
                   ......
                }
             }
    
    private void MergeFiles()
            {
                lock (lockObject)
                {
                   .............
                }
             }
    
    static void ImportFile(object import)
            {
               ..Code with no lock because foreach loop is already locked?...
               .... this code takes an hour to finish for each tickFile in tickCollection
            }
    Last edited by techtalk7; January 17th, 2012 at 05:42 PM.

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: MultiThreading Help need please

    Take a look at the BackgroundWorker class. It may suffice and is pretty straightforward (and robust) to use. It can simplify the code required for most common multi threaded needs.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Oct 2011
    Posts
    17

    Re: MultiThreading Help need please

    Quote Originally Posted by rliq View Post
    Take a look at the BackgroundWorker class. It may suffice and is pretty straightforward (and robust) to use. It can simplify the code required for most common multi threaded needs.
    This worked beautifully, thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured