hi techies,
i am developing a c# windows applications.
my requirement is similar to below
void myfunction()
{
// here i am calling another function
createreport(id,name,num);
//rest of the code
}
if createreport function takes more than 1 minute to execute,i want to stop that method and continue rest of the code.
please suggest me how can i do..thanks in advance
Loading


EhteshamRank #3,979 · 374 reputationPosted May 24, 2012, 10:36 PM EDT
Jignesh TrivediRank #16 · 50.3K reputationPosted May 24, 2012, 11:12 PM EDT
I think you can use threading in this case.
Try...
Example Defination of CreateReport is..
public static void CreateReport()
{
Thread.Sleep(7000); // use to hold value up to 7 sec
}
now try...
Console.Write("Jignesh Test");
Task t1= Task.Factory.StartNew(CreateReport);
t1.Wait(5000);
Console.Write("Jignesh Test");
"Wait" methos wait for 5 sec, if youe callback not comming within 5 sec then it run rest of the code.
hope this will help u.
sandeep raju dantuluri53 reputationPosted May 25, 2012, 12:35 AM EDT
can we do this in framework 3.5..?
EhteshamRank #3,979 · 374 reputationPosted May 25, 2012, 1:05 AM EDT
delegate void AddDelegate(int Max);
void Add(int Max)
{
for (int Index = 0; Index < Max; Index++)
{
System.Threading.Thread.Sleep(1000);
}
}
private void button1_Click(object sender, EventArgs e)
{
AddDelegate DelegateObj = new AddDelegate(this.Add);
IAsyncResult Result = DelegateObj.BeginInvoke(100, null, null);
//waits for 1 min
Result.AsyncWaitHandle.WaitOne(60000, true);
//you other code here
//
//
//
//
DelegateObj.EndInvoke(Result);
}
Jignesh TrivediRank #16 · 50.3K reputationPosted May 25, 2012, 1:20 AM EDT
just replace two line in my code..
var t1 = new Thread(CreateReport); //Task.Factory.StartNew(CreateReport);
Thread.Sleep(5000);
but it always wait for 5 sec.
hope this help.
EhteshamRank #3,979 · 374 reputationPosted May 25, 2012, 1:27 AM EDT
With do respect, what this code is going to do
var t1 = new Thread(CreateReport); //Task.Factory.StartNew(CreateReport);
Thread.Sleep(5000);
As their is no constructor defined for Thread which directly takes Method Address and also you are not starting the thread plus you are pausing(sleep) the primary thread not t1 which you planned to run CreateReport on
Jignesh TrivediRank #16 · 50.3K reputationPosted May 25, 2012, 2:17 AM EDT
Yes my friend, I forgot start thread..
Thanks A lot.
@Sandeep
Try..
var t1 = new Thread(CreateReport);
t1.Start();
Thread.Sleep(5000);
hope this help.