Multithreading i .net application
Hej med jer.Jeg har en applikation, der udfører en masse arbejde. Nu skal applikationen udvides med en service, der henter addressen for et givet telefon nr. Denne funktionalitet skal ske i baggrunden, så applikationen ikke skal vente på resultatet.
Jeg har udviklet nedenforstående kode, men når jeg lader tråden sove, hænger hele applikationen.. Det kunne tyde på at applikationen venter på at tråden bliver færdig alligevel. Hvad gør jeg forkert?
private void StartLookupAddressThread(string PhoneNo)
{
System.Threading.Thread th = new System.Threading.Thread(
new System.Threading.ParameterizedThreadStart(LookupAddresss));
th.SetApartmentState(System.Threading.ApartmentState.MTA);
th.Start(PhoneNo);
}
public delegate void MyDelegateMethod(object TelePhone);
public void LookupAddresss(object TelePhone)
{
if (this.InvokeRequired)
{
MyDelegateMethod theDelegateMethod = new MyDelegateMethod(this.LookupAddresss);
this.Invoke(theDelegateMethod, TelePhone);
}
else
{
try
{
// Removes any occurences of +,-,space,< or >
string phoneNo = System.Text.RegularExpressions.Regex.Replace(
TelePhone.ToString(), "
- +|
- +|[<]+|[>]+|[ ]+", "");
- +|[<]+|[>]+|[ ]+", "");
// Locks the address form resource for concurrency
lock (AddressForm)
{
// Any numbers left?
if (nn.LookupAddress(true))
{
AddressForm.ShowAddress(nn.Address);
if (!AddressForm.Visible)
AddressForm.Show(this);
this.Focus();
}
else
AddressForm.ShowAddress("");
}
System.Threading.Thread.Sleep(10000);
}
catch (Exception ex)
{
Debug.HandleException(ex);
}
}
}
private void buttonFindAddress_Click(object sender, EventArgs e)
{
StartLookupAddressThread(comboBoxDialNumbers.Text);
}