PDA

View Full Version : [CLOSED] How to handle a callback using background threads?



posser
Sep 05, 2014, 6:41 PM
Hi everyone!

I would like to know how can I create new a thread to execute slow tasks and handle a callback.
In the example below I have 3 buttons, the first do a slow task in the main thread, while the third do the same task but with a new thread. Notice that the second button waits the slow one to execute a fast job, an this is what I want to avoid with a new thread. I understand why this behaviour happens, I Just want a suggestion how can I easily parallelize server jobs without stuck the client.

Thanks.



<%@ Page Language="C#" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
public void slowTask()
{
Thread.Sleep(5000);
btnSlowTask.Enable();
btnSlowTask.Icon = Icon.ControlPlay;
btnSlowTask.Text = "Start Slow Task";
}
public void fastTask()
{
System.Threading.Thread.Sleep(1000);
btnFastTask.Enable();
btnFastTask.Icon = Icon.ControlFastforward;
btnFastTask.Text = "Start Fast Task";
}

[DirectMethod]
public void startSlowTask()
{
slowTask();
}
[DirectMethod]
public void startFastTask()
{
fastTask();
}

[DirectMethod]
public void startSlowTaskWithThread()
{
Thread slowTaskThread = new Thread(new ThreadStart(slowTask));
slowTaskThread.Name = "ThreadFuncSlowTask";
slowTaskThread.Start();
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Question</title>
</head>
<body>
<form id="form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server" />
<ext:Button ID="btnSlowTask" runat="server" Text="Start Slow Task" Icon="ControlPlay">
<Listeners>
<Click Handler="this.disable();this.setIconCls('#ControlStop');thi s.setText('Running...');App.direct.startSlowTask() ;" />
</Listeners>
</ext:Button>
<ext:Button ID="btnFastTask" runat="server" Text="Start Fast Task" Icon="ControlFastforward">
<Listeners>
<Click Handler="this.disable();this.setIconCls('#ControlStop');thi s.setText('Running...');App.direct.startFastTask() ;" />
</Listeners>
</ext:Button>
<ext:Button ID="btnSlowTaskWithNewThread" runat="server" Text="Start Slow Task With New Thread" Icon="ControlPlay">
<Listeners>
<Click Handler="this.disable();this.setIconCls('#ControlStop');thi s.setText('Running...');App.direct.startSlowTaskWi thThread();" />
</Listeners>
</ext:Button>
</form>
</body>
</html>

Vladimir
Sep 05, 2014, 7:20 PM
Hi

May be that example can help you
http://examples2.ext.net/#/Miscellaneous/ProgressBar/Server_Side_Update/

geoffrey.mcgill
Sep 05, 2014, 7:51 PM
I think the following thread is also going to be very helpful:

http://forums.ext.net/showthread.php?16895&p=72625&viewfull=1#post72625

posser
Sep 05, 2014, 8:03 PM
Thank you guys!