C# multithreading code not hitting break point -
please @ sample code below, when place break point inside "testmethod" , run application, not hitting breakpoint. code good?
static void main(string[] args) { thread[] testthreads = new thread[3]; list<int> testdata = new list<int>(); testdata.add(1); testdata.add(2); testdata.add(3); int = 0; foreach (int data in testdata) { testthreads[i] = new thread(new threadstart(() => testmethod(data))); testthreads[i].name = string.format("working thread: {0}", data); i++; } } static void testmethod(int i) { try { //datatable dt = db.getdata(i); if (dt.count > 0) { console.writeline("count: {0}", dt.count); } } catch (exception ex) { throw ex; } }
i tried method , returns name of first input.
method 2:
static void main(string[] args) { list testdata = new list(); testdata.add(111111); testdata.add(222222); testdata.add(333333);
foreach (int data in testdata) { new thread(delegate() { datatable dt = db.getdata(data); if (dt.count > 0) { console.writeline("name: {0}", dt.rows[0]["name"];); } // signal countdownevent. countdownevent.signal(); }).start(); } // wait workers. countdownevent.wait(); console.writeline("finished.");
}
output:
name: julie name: julie name: julie
always returns value(name) of 111111
you need start thread.
testthreads[i] = new thread(new threadstart(() => testmethod(data))); testthreads[i].name = string.format("working thread: {0}", data); testthreads[i].start();
Comments
Post a Comment