Sep 05, 2014, 6:41 PM
[CLOSED] How to handle a callback using background threads?
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.
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');this.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');this.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');this.setText('Running...');App.direct.startSlowTaskWithThread();" />
</Listeners>
</ext:Button>
</form>
</body>
</html>
Last edited by Daniil; Sep 06, 2014 at 8:26 AM.
Reason: [CLOSED]