|
Dear C# enthusiasts,
I am currently developing a C# application that controls some test equipment and performs functional tests on a product. This type of application is inherently sequential in nature so I have thus far avoided muddying the water with issues of multi-threading.
However, I am dissatisfied with my programs because the UI is not responsive while my functional test loop is running. At the moment the limit of my programming skill only allows for dirty calls to Application.DoEvents() in order to process data on my UI as the test is executing.
It seems, then, that whilst my program is inherently sequential I would benefit from having the UI and the test loop running concurrently. And now we arrive at the core of my question:
What multi-threading method is the path of least resistance for this kind of application given that I am not very experienced in C#?
So far I have read about the background worker and tasks. My understanding is that tasks are the way forwards for .net developers and that it's becoming very important to have a grasp of them. I read some articles about it here on Code Project but unfortunately I have found them difficult to follow. That is a reflection upon my level of competency, not the articles themselves.
I am willing to persevere with tasks to gain full understanding but only if that knowledge is likely to be of significant benefit to me in the future.
Advice and comments are welcome.
Thanks,
Brian.
Electronics Engineer, Computer Programmer and general GEEK.
|
|
|
|
|
|
Brian, see this[^] article. BackgroundWorker may be exactly what you need to quickly get your test harness app to be responsive.
/ravi
|
|
|
|
|
Thank you for the replies both. I take your points and I will be familiarising myself with the background worker over this coming weekend.
By the way, I have heard a few recommendations for Head First C# (the source of the linked article) and I think perhaps I should grab myself a copy.
Kind Regards,
Brian.
|
|
|
|
|
I haven't been impressed by the HeadFirst series. IMHO, they tend to oversimplify the material and add unnecessary fluff to the subject. I recommend Programming C#[^] (O'Reilly Press).
/ravi
|
|
|
|
|
Thank you for the book recommendation Ravi. I have just purchased Head First C# via Google Books and already I can see what you mean by "unnecessary fluff". It's probably a good book for someone who is not only new to C#, but new to programming completely. I'm not new to programming and I'm not completely new to C# either. I'm somewhere in between beginner and amateur as far as C# is concerned I think. Still, I don't mind having a beginner's book to hand because sometimes that's just what you need when you're trying to find out about some particular aspect of something.
I have had success with other books in the O'Reilly Press series. Namely Learning Perl, Intermediate Perl, and Mastering Regular Expressions.
I think I will give your book recommendation a whirl!
Thanks again,
Brian.
|
|
|
|
|
Background workers will solve this issue for you.
They are real easy to use.
You just have some limits on how you interact with them..
But, by default your UI will run in the same thread and it will freeze if you have some other code that fired off from the ui with no threading.. and that is the problem you a experiencing.
You can use a background worker and fire off the thread and the Dowork sub will then run in the background and will not freeze the ui. I would suggest you read up a little on background workers but they can help a ton with UI freezing issues.
=)
|
|
|
|
|
I throughly recommend this[^] website. Its articles on threading are among the very best I've read.
If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.
|
|
|
|
|
Hi All,
First time posting here, long time programmer..
So, I have a need for custom controls that I am using to dynamically render a list with call backs..
So, far I had no problem doing simple inherited based controls and hooking their callbacks...
IE CusControl : TextBox...
Then its fairly easy to hook the TextChanged event and add the controls a dynamic list and catch the post backs.
Now, the hard part is when I am no longer inheriting the TextBox base control.. but instead I want to create my own control (combo of many controls) and expose my own events to the page that contains my custom control.
For example..
I have a class, and inside the class I create my own controls.
example of the idea..
public class CusParamTextBox : BaseParam
{
public TextBox ParamTxtBox;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateCustomChildControls();
}
private void CreateCustomChildControls()
{
ParamTxtBox = new TextBox();
ParamTxtBox.AutoPostBack = true;
ParamTxtBox.TextChanged += new EventHandler(ParamTxtBox_TextChanged);
base.Controls.Add(ParamTxtBox);
}
public void ParamTxtBox_TextChanged(object sender, EventArgs e)
{
TextBox cuscontrol = (TextBox)sender;
ParamTxtBox.Text = "fired!";
}
}
Ok, so now I want to expose another event ExposedUpdate() outside of this class..
kind of like a universal update event that I want to be able to hook to the page that loads this control.
So.. inside my ASP.NET Page that contains the custom control I want to hook my own event somehow.. something like this...
CusParamTextBox tmpctrlx = new CusParamTextBox();
tmpctrlx.ExposedUpdate() += new EventHandler(this.Update);
protected void UpdateText(object sender, EventArgs e)
{
CusParamTextBox cuscontrol = (CusParamTextBox)sender;
DosomethingOnPage(cuscontrol.name,cusontrol.value);
upMain.Update();
}
The idea would be, that the local event fires off on TextChanged then fires off the other ExposedEvent that would then be hooked into the main page to fire off more code on the main page.
Any idea or am i off the rocker and there is a better way to do this ?
=)
|
|
|
|
|
In future, please edit you existing question to provide info instead of creating a new one...
This message is manufactured from fully recyclable noughts and ones. To recycle this message, please separate into two tidy piles, and take them to your nearest local recycling centre.
Please note that in some areas noughts are always replaced with zeros by law, and many facilities cannot recycle zeroes - in this case, please bury them in your back garden and water frequently.
|
|
|
|
|
I think between you, me and Pete, the point has been made.
Use the best guess
|
|
|
|
|
He said "Edit", not Repost. For future reference, there is an Edit link at the bottom of each of your messages.
Use the best guess
|
|
|
|
|
The site let me post twice during the process of writing a single post.
There was no confirm, and when I hit a hotkey and my work posted instantly..
Then I clicked back and it let me edit the text I was working on and then when I clicked post again it created a new post and posted again instantly.
Ultimately the problem is both user error on mypart and also software error on the forums part.
I am new, so I did not realize what just happened.. and the software did not detect that I posted twice from the same exact page...... I quick guid check or confirm could of taken place to prevent me from double posting.. And I could of stopped it if I realized it.
So, I apologize for my mistake, but I cant take all the credit!
=)
|
|
|
|
|
|
Member 4632726 wrote: Then I clicked back and it let me edit the text I was working on and
If you had hit "Post Message" without editing, our system would have rejected the duplicate post. As it stands, you edited the text and hit send so our system thinks you simply posted a new, different message.
When you posted the original message you would have seen, just under your message, links to edit or delete your message.
We will be making changes that will make it even harder to repost accidentally.
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
So you just want to expose an event from your custom control?
public class CusParamTextBox : BaseParam
{
public event EventHandler ExposedUpdate;
protected virtual void OnExposedUpdate(EventArgs e)
{
var handler = ExposedUpdate;
if (handler != null) handler(this, e);
}
...
private void ParamTxtBox_TextChanged(object sender, EventArgs e)
{
...
OnExposedUpdate(e);
}
}
...
var tmpctrlx = new CusParamTextBox();
tmpctrlx.ExposedUpdate += Update;
...
protected void Update(object sender, EventArgs e)
{
var cuscontrol = (CusParamTextBox)sender;
DoSomethingOnPage(cuscontrol.name, cuscontrol.value);
upMain.Update();
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
modified 14-Aug-13 10:02am.
|
|
|
|
|
Yes, I want to expose a event that I can hook too.
So, I can tell it to fire in the local control... but pretty much do nothing locally... but then hook it to a page event that will do the heavy lifting.
=)
|
|
|
|
|
So, pretty much exactly the example I posted then?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yes, Thank you so much!
That is working like a charm!
=)
|
|
|
|
|
Ok, I do have a new problem now..
It seems when I put this code into my update panel...
It continues to do a full post back all the time...
Any idea how how to solve this any one ?
Thanks!
=)
|
|
|
|
|
Have you added an AsyncPostBackTrigger[^] for the control?
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<uc:CusParamTextBox ID="CusParamTextBox1" runat="server" OnExposedUpdate="Update" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="CusParamTextBox1" EventName="ExposedUpdate" />
</Triggers>
</asp:UpdatePanel>
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I did try something like that, but a little different because I am adding the controls dynamically to a existing update panel.
I did try but with no luck..
CusParamTextBox tmpctrlx = new CusParamTextBox();
tmpctrlx.ExposedUpdate += new EventHandler(UpdateText);
ScriptManager1.RegisterAsyncPostBackControl(tmpctrlx);
CusParamControls.Controls.Add(tmpctrlx);
and on the page that contains the dyn control
<asp:UpdatePanel runat="server" ID="upMain" Visible="true" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel runat="server" ID="CusParamControls"></asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
=)
|
|
|
|
|
Sorry, I'm not overly familiar with the UpdatePanel . You might be better off posting this new issue in its own thread.
NB: It would be better to post the question in the ASP.NET forum[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
modified 14-Aug-13 14:30pm.
|
|
|
|
|
Hi Rich,
Its ok, I manged to figure it out..
I wanted to add the solution to the post too so other may do the same.
Right after your add your control, and then your event via the init...
If you want to put your control inside a update panel and stop a full page post back then you must register your control with the parent page and do it inside the actually control code itself... outside dont seem to work via the main page containing the control.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ScriptManager script = ScriptManager.GetCurrent(Page);
TxtParamValue = new TextBox();
TxtParamValue.TextChanged += new EventHandler(TxtParamValue_TextChanged);
if (script != null) { script.RegisterAsyncPostBackControl(TxtParamValue);}
base.Controls.Add(TxtParamValue);
}
Thanks, and enjoy all!
=)
|
|
|
|
|
Hi All,
First time posting here, long time programmer..
So, I have a need for custom controls that I am using to dynamically render a list with call backs..
So, far I had no problem doing simple inherited based controls and hooking their callbacks...
IE CusControl : TextBox...
Then its fairly easy to hook the TextChanged event and add the controls a dynamic list and catch the post backs.
Now, the hard part is
=)
|
|
|
|