PDA

View Full Version : [CLOSED] Multi threading.



Prasoon
Jul 31, 2015, 11:17 AM
i have a tab panel, from one tab can i set value of a label of another tab, using a child thread?
Need your guidance.

regards,
Prasoon

fabricio.murta
Aug 01, 2015, 8:14 AM
Hello @Prasoon!

Yes, you can! Not really sure how much the 'multithread' term fits but, well, here are some examples for different components communicating in our examples explorer:
- Communication from children iframes (children pages communicating to each other) (http://examples2.ext.net/#/Panel/Basic/IFrame_Communication/)
- DirectMethod (server-side) command changing a label (http://examples2.ext.net/#/Events/DirectMethods/Overview/) -- this could be triggered from any control within the page.
- Using asynchronous services (asmx) to change controls (http://examples2.ext.net/#/TreePanel/Advanced/Remote_Mode_with_Service/) -- right-click the nodes to see the change operations available.

I hope this helps!

Prasoon
Aug 01, 2015, 3:04 PM
Thanks fabricio, i am able to set values of the control using child thread (controls which are on the other tab of tabpanel), but when i click on that tab, values are not updated, is there any way to refresh/reload tab, so that it can show the values set by the child thread.
Im sorry, i cant share sample code as it is complex with lot of dependency.

fabricio.murta
Aug 01, 2015, 7:30 PM
Hello @Prasoon,

Sorry, I think I wasn't clear enough. We here at Ext.NET are not allowed to ask you for your proprietary code, and we never mean that. What we can ask for is a sample code that reproduces the issue. Furthermore, if we were to understand the complex applications our customers make, all the information there would just divert us from the actual problem.

That's why we keep asking for a sample to reproduce the issue users have. Not to peek on your work, but to be able to quickly reproduce your problem at our side with no distractions and focus on the best reply.

Usually in that cases we can just point a line that might be changed in order to fix the issue, and often we also suggest a fix with the whole sample review, based on the provided sample and description of the expected behavior.

Look at this recent thread, you'll see the question looked complex but, with the provision of the code we could study it in detail and provide a fix:
How to load data on store and set Total of pagingtoolbar in client side for static direct method (http://forums.ext.net/showthread.php?59830/)

I will write a simple example what I think you want to do and will post below in a while.

Sorry for the misunderstanding, and I am looking forward to be able to help you in your issue.

fabricio.murta
Aug 01, 2015, 8:19 PM
As promised, the sample:


<%@ Page Language="C#" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
var counter = 0;

function updateLabel(which) {
var target = Ext.getCmp(which);
if (target && target.xtype == 'netlabel') {
target.setText('Updated. Counter: ' + counter++);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<ext:ResourceManager runat="server" SourceFormatting="true" ScriptMode="Debug" />
<ext:TabPanel runat="server" ID="tabPanel1">
<Items>
<ext:Panel runat="server" ID="masterTab" Title="Master Tab">
<Items>
<ext:Button runat="server" ID="mtButton1" Text="Change Tab1" OnClientClick="updateLabel('tab1Label')" />
<ext:Button runat="server" ID="mtButton2" Text="Change Tab2" OnClientClick="updateLabel('tab2Label')" />
</Items>
</ext:Panel>
<ext:Panel runat="server" ID="tab1" Title="Tab1">
<Items>
<ext:Label runat="server" ID="tab1Label" Text="Value unchanged." />
</Items>
</ext:Panel>
<ext:Panel runat="server" ID="tab2" Title="Tab2">
<Items>
<ext:Label runat="server" ID="tab2Label" Text="Value unchanged." />
</Items>
</ext:Panel>
</Items>
</ext:TabPanel>
</div>
</form>
</body>
</html>


It illustrates how controls in one tab can change labels on other tabs using JavaScript events. You could, in an analogue approach, use direct methods to do so. Check the directMethod example I provided on my first reply (http://examples2.ext.net/#/Events/DirectMethods/Overview/) for more on it.

Prasoon
Aug 02, 2015, 7:08 AM
Appreciate your effort for understanding me and all your effort in resolving my issue :), thanks a lot for sample code.
i tried too and coded a sample for you, may be, it will help you in understanding better and again i will get a change to learn some thing new from you guys.

Please follow below STEPS to execute

1) include using System.Threading;
2)arrange the code below in aspx and respective .cs file.
3)execute and click on the button, as you click on button it fires button click event and creates a child thread, which in return changes label (Label2) text, now click on 2nd tab, label values is not changes, it is shows old value.

may be i am not confusing you :)



--------------------------------------------------Client side Code-----------------------------------------------------------
<body>
<ext:ResourceManager ID="ResourceManager1" runat="server" />
<form id="form1" runat="server">
<div>
<ext:Button runat="server" ID="imButton" Text="Please hit me...">
<DirectEvents>
<Click OnEvent="Unnamed_Event">
</Click>
</DirectEvents>
</ext:Button>
<ext:TabPanel runat="server" Width="900"
Height="900"
MarginSpec="0 0 20 0"
Plain="true" ID="imMain">
<Items>
<ext:Panel runat="server" Width="800px" Height="800px" Title="im 1st panel" AutoDataBind="true">
<Content>
<ext:Label runat="server" Text="prasoon" ID="Label1"></ext:Label>
</Content>
</ext:Panel>
<ext:Panel ID="TabPanel1" runat="server" Width="800px" Height="800px" Title="im 2nd panel" AutoDataBind="true">
<Content>
<ext:Label runat="server" Text="please change me using thread...." ID="Label2"></ext:Label>
</Content>
</ext:Panel>
</Items>
</ext:TabPanel>
</div>
</form>
</body>
----------------------------------------------------------------Server side code-------------------------------------------------------
public void callme()
{
Label2.Text = "-------------------prasoon----------------------";
}

public void Unnamed_Event(object sender, DirectEventArgs e)
{
ThreadStart childThread = new ThreadStart(callme);
Thread reportThread = new Thread(childThread);
reportThread.Name = "-----PK-----";

reportThread.Start();
}

fabricio.murta
Aug 02, 2015, 11:56 PM
Oh, you are starting a thread from the code behind!..

Well, that would not be an Ext.NET limitation, but rather a limitation inherent from ASP.NET pages' life cycle, that server-side cannot really initiate communication with the client. There always must be the client on the initiating side of anything that will change the client's view. That's related to the 'ASP.NET page life cycle'.

As for the literature for this, its here: ASP.NET Page Life Cycle Overview (https://msdn.microsoft.com/en-us/library/ms178472.aspx).

As workarounds to this limitation it would be necessary to make your thread change a session variable (I am not 100% sure it would be on scope when you instantiate concurrent task) or something which a timed client-side task probes before performing a task or updating the control.

Assuming the session can be touched from the task, you can keep a variable to indicate new label value as 'null' and once you call the code behind to add the task, also add a recurrent AJAX or direct call to probe that variable until it has value.

Once it has a value, for example, the same direct call could perform whatever is wanted once the long running task is done or (little worse) return "true" so another dedicated task is called to collect the results and update the controls. And as the task is fulfilled, then stop the recurring call to the code behind background action.

I have worked in an Ext.NET project which used services handers (ashx) to perform this task, although I do not remember in details how it did that.

Alright, enough blah-blah-blah. Here's an example we have that implements something like what I'm saying. And it uses a session variable as the trigger.
Progress bar - server side update (http://examples2.ext.net/#/Miscellaneous/ProgressBar/Server_Side_Update/)

I hope I am finally able to help you! :) Just give a word otherwise.

Prasoon
Aug 04, 2015, 11:25 AM
Thanks for your effort and guidance, you were correct, it was noting to do with Ext controls, it was the issue by the way i was trying to use thread (to make any UI changes from child thread, we need to initiate UI thread). there is a BackgroundWorker class which is build on threads, this class helps in using threading concept in a easier way. By BackgroundWorker i was able to change UI.

Sharing sample code..




----------------------------------------------------aspx code----------------------------------------------------------------
<body>

<ext:ResourceManager ID="ResourceManager1" runat="server" />
<form id="form1" runat="server">
<div>
<ext:Button runat="server" ID="imButton" Text="Please hit me...">
<DirectEvents>
<Click OnEvent="Unnamed_Event" >
</Click>
</DirectEvents>
</ext:Button>
<ext:TabPanel runat="server" Width="900"
Height="900"
MarginSpec="0 0 20 0"
Plain="true" ID="imMain">
<Items>
<ext:Panel runat="server" Width="800px" Height="800px" Title="im 1st panel" AutoDataBind="true">
<Content>
<ext:Label runat="server" Text="prasoon" ID="Label1"></ext:Label>
</Content>
</ext:Panel>
<ext:Panel ID="TabPanel1" runat="server" Width="800px" Height="800px" Title="im 2nd panel" AutoDataBind="true">
<Content>
<ext:Label runat="server" Text="please change me using thread...." ID="Label2"></ext:Label>
</Content>
</ext:Panel>
</Items>
</ext:TabPanel>
</div>
<ext:Hidden runat="server" Text="0" ID="hdnpleasecheck"></ext:Hidden>
</form>
</body>
----------------------------------------------------------------.cs code---------------------------------------------------------------------------
BackgroundWorker m_oWorker;

protected void Page_Load(object sender, EventArgs e)
{
}

public void callme(object sender, DoWorkEventArgs e)
{
int a = 1;
m_oWorker.ReportProgress(0);
}
void m_oWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Label2.Text = "Processing......" ;
}

public void Unnamed_Event(object sender, DirectEventArgs e)
{
m_oWorker = new BackgroundWorker();

m_oWorker.DoWork += new DoWorkEventHandler(callme);
m_oWorker.ProgressChanged += new ProgressChangedEventHandler
(m_oWorker_ProgressChanged);

m_oWorker.WorkerReportsProgress = true;
m_oWorker.WorkerSupportsCancellation = true;

m_oWorker.RunWorkerAsync();

}



please mark this thread as closed.

regards,
Prasoon

fabricio.murta
Aug 04, 2015, 12:38 PM
Glad you were able to address your issue! These background jobs libraries can really help to implement such functionalities avoiding some of the common parallelism troubles, I think you made a wise choice.

Thank you very much also for sharing the outcome!