以下のコードでは、フォーム上のボタンにドラッグアンドドロップされたファイルを取り出し、スレッドで処理しています。 foreachループが継続され、次のファイルを処理する前に、各スレッドがその操作を完了できるようにしたい。スレッドが開始パラメータを持つときにスレッドを開始して結合する
私は試しました
testthread()。Join();
右
新しいThread(()...
が、それは私が最初にスレッドを起動したとき、私はtestthreadに渡すのと同じパラメータを渡すために私を望んでいるので、それがエラーを取得します。
後の缶誰かしてくださいそれは方法Cを持っている何かを返す期待し、私が?
private void btnClick_DragDrop(object sender, DragEventArgs e)
{
string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
string ButtonName = "TestButton"
string[] files = new string[10];
files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
FileInfo fileInfo = new FileInfo(file);
Console.WriteLine("++ Filename: " + fileInfo.Name + " Date of file: " + fileInfo.CreationTime + " Type of file: " + fileInfo.Extension + " Size of file: " + fileInfo.Length.ToString());
string CleanFileName = System.Web.HttpUtility.UrlEncode(fileInfo.Name.ToString());
//Start thread
try
{
Console.WriteLine("++ Calling testthread with these params: false, " + ButtonName + "," + CleanFileName + "," + file);
new Thread(() => testthread(false, ButtonName, CleanFileName, file)).Start();
testthread().Join(); //THIS DOES NOT WORK BECAUSE IT WANTS THE PARAMETERS THAT THE THREAD IS EXPECTING. WHAT CAN I PUT HERE SO IT WAITS FOR THE THREAD TO FINISH BEFORE CONTINUING THE FOREACH LOOP ?
}
catch (Exception ipwse)
{
Console.WriteLine(ipwse.Message + " " + ipwse.StackTrace);
}
}
}
public void testthread(bool CalledfromPendingUploads, string ButtonName, string CleanFileName, string FilePath)
{
//My Code to do the file processing that I want done. I do not want multiple threads to run at once here. I need the thread to complete, then the foreach loop to continue to the next file and then start another thread and wait, etc...
}
スレッドを開始してから待機するのは無意味です。 testthread()を直接呼び出してください。 –