다음 코드는 C# 4.0을 사용하여 다중 스레드로 변환하려고합니다. 그렇게 할 수 있습니까? 모든 지침을 크게 주시면 감사하겠습니다.단일 스레드 C# 코드를 다중 스레드로 변환
나는 버튼 시작 프로세스를 시작해야하며 먼저 다음 몇 가지 이
을 멀티 스레딩 할 논리SomeJobA();
SomeJobB();
SomeJobC();
...
을 분할 다음과 같은 기능을
private void ProcessData()
{
//clear some ui text fields and disable start button and enable cancel button and set status to working
//open database connection
try
{
//populate ui multi line textbox saying that it is getting data from database
var dsResult = new DataSet();
//populate dataset
//populate ui multi line textbox saying that it finished getting data from database
//close connection
if (dsResult.Tables.Count == 1 && dsResult.Tables[0].Rows.Count > 0)
{
//populate another field saying how much records we got
int iCount = 1;
foreach (DataRow dr in dsResult.Tables[0].Rows)
{
if (_stop)
{
//set the status as forced stop
return;
}
//populate the currently processed record count using iCount
//populate ui multi line textbox indicating which item that it is starting to work using dr["Item"]
//call some external function to process some data, inside this function i have to update ui multi line textbox as well
var dataFile = SearchDataFile(dr["Item"].ToString());
if (dataFile == null)
{
//populate ui multi line textbox indicating that item was not found
iCount++;
continue;
}
//call another external function to process some data, inside this function i have to update ui multi line textbox as well
UpdateDataFile(dataFile, folderId, dr, dr["Item"].ToString());
iCount++;
}
}
else
{
//populate ui multi line textbox indicating no data found
}
//update status saying that it is complete
tsslblStatus.Text = "STATUS : COMPLETE";
}
catch (Exception ex)
{
//close connection
//populate ui multi line textbox indicating error occured
//update status to error
}
finally
{
//re adjust ui and enabling start and disable stop
//set _stop variable to false
}
}
감사
당신은 어떤 속도 저하가 발생하는 :
당신은 당신이 병렬로 수행 할 작업을 마샬링하는
TaskFactory
를 사용할 수 있습니까? 어느 부분으로 우리가 움직이기를 원합니까? – Complexity멀티 스레딩 및 GUI 응용 프로그램에주의하십시오. 멀티 스레딩은 GUI가 응답하지 않을 수 있습니다. – BossRoss
@BossRoss 왜 그런가요? – Sinatr