|
-
January 17th, 2012, 05:25 PM
#1
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|