0

I have in my code two same and big pieces of code, so i create a method and put it there. I call the method two times in my code, the first time normally and the second time i want to call it from doWork event (background worker). The problem is that when it executes (in doWork) my program stops responding... So my question is.How can i call a method from doWork event?

Edit: Some code

private void parseHtmlFile(string file)
{
// here i parse an html file and fill some arraylists.
}

// this is the event where i call method on click
private void MenuItem_NewPr(object sender, RoutedEventArgs e)
{
parseHtmlFile(file);// in this case program works well.
}

// do work 
worker.DoWork += (s, e) =>
{
var file = e.Argument as string;
parseHtmlFile(file);// in this case program stop responding.
}

Edit 2: I changed some thing in method and now it doesn't crash, but i have another problem now. The dowork method doesn't executes code which exists below my method call statement. I put a breakpoint there and never goes there...

user1005633
  • 625
  • 3
  • 9
  • 23
  • 2
    Well what does that method do? It would be really helpful if you'd provide a short but complete program demonstrating the problem. – Jon Skeet Mar 18 '13 at 09:51
  • The method do an html parse. The problem is that the application stops responding when call this method from dowork, no debug errors.. – user1005633 Mar 18 '13 at 10:27
  • 1
    Again, you'll need to provide some actual code. For example, if you're accessing the UI from a non-UI thread, that's a bug. – Jon Skeet Mar 18 '13 at 10:28
  • I understand but i don't know what should to provide. I just replace the code in dowork event with call method statement, which(method) contains the exact same code.. Thanks! – user1005633 Mar 18 '13 at 10:50
  • You should provide a short but complete program demonstrating the problem, ideally. – Jon Skeet Mar 18 '13 at 11:01
  • ok i add some code, if you want something more let me know... – user1005633 Mar 18 '13 at 12:01
  • Can you show us the code of parseHtmlFile ? – Xaruth Mar 18 '13 at 12:19
  • 1
    Deadlock is very common in code like this, HTML parsers are never thread-safe. Check [this answer](http://stackoverflow.com/a/4271581/17034) for a typical approach. And do make sure to **never** wait for the BGW to complete, that causes deadlock as well. – Hans Passant Mar 18 '13 at 12:26
  • I changed some thing in method and now it doesn't crash, but i have another problem now. The dowork method doesn't executes code which exists below my method call statement. I put a breakpoint there and never goes there... – user1005633 Mar 18 '13 at 15:15

0 Answers0