Threading In a WinForm [closed] - multithreading

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Everything in my app is working like I want but I don't really understand how exactly? I setup a couple of threads to monitor some resources the app will need from time to time. The threads update a couple of labels saying "all good" or "uh oh"(not really but you get the idea). I setup the test of InvokeRequired to make sure it executes on the thread that created the labels and everything works great.
What I don't understand is... If i'm typing into a a textbox while the labels are being updated shouldn't i see some kind of lag in my typing since it's on the same thread or at least have to implement some code so i can continue typing where i left off? My guess is that the Invoke method takes care of things so i can continue typeing where i left off.
It's working so I shouldn't complain but I don't want some bug to popup a month from now that I really don't understand.

Short answer: there is nothing to worry about.
Explanation: Pretty much every GUI system in existence, including WinForms, is single-threaded, so nothing happens simultaneously, but also consider that most operations are lightning quick too (even painting).
A better explanation would explain how the GUI message loop works, but I'm too lazy to explain it, but I will explain the part you say you don't understand. This is the sequence of events when you type into a textbox:
Keypress message received by the root window (by its message loop)
Keypress message is dispatched to the textbox from the root window
Keypress message received by textbox. Textbox updates its internal state by appending the new character to its internal Value string.
The textbox informs the parent window that the texbox needs to be repainted at the next nearest opportunity because its text value has changed.
The root window gets the next message: which is a request to perform a method invokation (because Invoke or BeginInvoke was called from another thread).
The root window executes this invoke method.
In this case, your Invoke'd code, as far as I can tell, just sets the Text property of a label, this operation is quick as it just sets the internal state of the label, it does not cause it to be painted directly, instead (as in step 4), it marks the label as "invalidated" and must be repainted at the next best opportunity (see step 8).
The root window gets the next message: a request to repaint the textbox
The root window instructs the textbox to repaint itself
The root window gets the next message, or sits idle waiting for the next message to be received
So yes, there is the possibility for lag or a delay in the message cycle, for example, if your Invoke'd code takes more than a few miliseconds to execute. If you add Thread.Sleep(10*1000) in your Invoke'd code, you'll see.

Related

Loop in a activity diagram

I am trying to design an activity diagram for an image editing application. Let's say that the application has one adjustment to edit an image. that's brightness. When the user opens the application he can change the brightness again and again. Then finally save it. That's really not a loop. but it's a repetitive process. How can I represent such a process. I have found stack answers for looping through documents and for loops. But didn't found a matching scenario for like this.
Thank you!
Oh, but why do you say it is not a loop? It is.
Sorry, no reasonable drawing tool at hand, so this will be textual
Let's look at a pseudocode:
open app (image as attribute)
while decide to continue to brighten the image do
brighten the image
loop
As you can see you do loop and your condition to loop or finish it depends on the decision to brighten or finish working with the app.
The brightening itself can be more complex (e.g. may have some selection of settings like level or method of brightening, it may even have the ability to break the brightenin or undoing) but it is still the loop.
Out of this solution to represent a loop you can use options 2 and 3 easily.

Moving to next question when failing to receive a response

We had a developer to come up with a prototype for a bot for bookkeeping questions and we understand that the bot is not perfect. Our biggest challenge was to ensure that after 2-3 failed attempts for the bot to receive an appropriate response, the bot moves on to the next question and that's it. Our previous developer claimed that it's not possible, is that true or not? Currently the bot just gives up after a couple of attempt and that's it.
I am not a tech person and I would really appreciate some help on this.
Hypothetical example of the ideal scenario:
Q: What accounting software do you use?
A: askdnjsajld
Q: Didn't get that. What accounting software do you use?
A: asdkjnajksdn
Q: I am sorry, didn't get that. Let's move on to the next question... When would you like to receive your financials?
A: month-end
Thank you for your help!
Yes, this is possible, although the exact details of how you do this depend on how you're handling responses from the user in the first place.
The most important thing to keep in mind to handle this, however, is to remember that Intents represent what the user says and not how you handle it or how you reply.
That last bit gives the simplest answer to your question - you can, of course, reply however you want the bot to reply after each round. It sounds like you were able to implement logic that says if it got a result it didn't want - you can extend that to add a counter that just uses the next question as its reply after a number of tries.
The more difficult part is to make sure that you know what question the user is replying to, and to make sure you capture the right value in case they try to go back and answer a previous question.
The general solution to this problem is twofold:
Have one Intent that accepts the direct answer to the question you're currently on, but have it triggerable only with certain Input Contexts being set. Then set that Context to be valid when you ask the question, and remove the Context when the question is answered.
Have other Intents that specifically respond to how the user might phrase the question if they were going back to answer it.
For example, if you have asked "What software do you use?" you might set the Context "ask-software-used". You would then have two Intents:
One with an Input Context of "ask-software-used" that accepts just valid software names.
Another with no Input Context, but which might have training phrases such as
"I'm using XXXX software"
"We are working with the XXXX package"
once the user does answer the question, clear the "ask-software-used" Context, ask the next question, and set its Context.
You can also use this to keep track of how many times you've had to repeat the question, waiting for an answer. If that counter hits the limit, do the same thing: clear the Context, ask the next question, and set its Context.

JAVAFX8 updating controls models?

UI Control such as LISTVIEW or Tree or ... comes with model that is observable.
When one make a change to that model, I suppose JavaFX knows how to refresh it automatically in the display.
However my question here is as follows:
Is it the intent way, that someone who wants to update and not replace this model, do so in a background thread with a platform.runlater.
In other words, one has some serious computation to do, and needs to to update an ObservableList as a result. Is it the intended way, to do the heavy work in a background thread and at the end of it, run the update in a platform run later?
I'm asking this because this is what I have been doing so far without problem. But from my reading here and there, in particular in
http://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html
It seems that some other mechanism shall be used. One should rather return a full list instead of updating the observable list.
But this works only if things comes from the GUI. In case the update is triggered from the back end, there is no way to do so.
The solution that I have used so far, was always to hold a reference to the observable list and updating it by means of platform.Runlater.
Is there any other way ?
The link you give has an example (the PartialResultsTask) that does as you describe: it updates an existing ObservableList as it progresses via a call to Platform.runLater(). So this is clearly a supported way of doing things.
For updating from the back end (i.e. from a class unaware that the data are being used in a UI), you'd really have to post some code for anyone to be able to help. But you might have a look at the techniques used in this article. While he doesn't actually update lists from the backend in the examples there, the same strategy could be used to do so.

How to make a series of dice rolls secure in a client/server scenario?

Imagine the following situation in a game: A series of random numbers is presented to the player. Each number is shown to the player for a short period of time before it goes to the next one. It is the player's aim to pick a high number. He or she just needs to 'click' in the right moment and then the number is chosen.
The question is about how to implement this scenario in a secure way in a client/server scenario.
That means there is a game client which displays the above mentioned scene and there is a server to which the chosen number (in whatever way) needs to be send. The catch is to get this thing secure so that cheating (e.g. by modifying the client) is not possible.
There's really no way to make this completely secure. Even without modifying your client, all someone needs to do is have another program running in the background reading the monitor and looking for a certain number to appear and then send a mouse click event to your client program. This can be done without modifying your client program at all. Even if you somehow managed to make it secure so that they couldn't run any other programs or services at the same time as your program, they could just point a webcam at the screen hooked to a different computer to do the optical digit recognition and send a mouse click event over USB to the computer running your program.
Fortunately, you may be able to get around the problem. Generally if something is to be a "dice roll" you want a random "luck based" outcome. By allowing them to click at a certain time you are making this a skill based game instead of luck based and therefore not really a dice roll. You could make it so there's a slight "delay" from when they click to when the dice stops rolling, so they see it rolling around, click, and it slows down and lands on a number. This way what number was displayed when they actually click does not determine the outcome, but the next (random) number after that one, which would eliminate the possibility of cheating and make this a luck based roll instead of skill based.

Is my "design pattern" for asynchronous methods good? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
When I'm writing a class which contains methods that are probably going to be executed on another thread, then I prepare my class for async usage.
I add events to the class as the following:
methodProgressChanged
methodCompleted
In the method(s), before every sub-operation (for example, iterations) I raise the ProgressChanged event with a custom EventArgs class. This custom class contains info about what sub-operation is beginning now, and what the result of the previous sub-operation was.
So executing the method on another thread is not the method's task, it's just reporting the progress.
And of course, when I execute my method, the caller takes care of subscriptions to the events.
Is this idea acceptable, or should I forgot it totally?
If you are in .NET 4.0, I would recommend looking into Task and Task to encapsulate your async functionality. Then you could return a Task from the method you want to be a like so:
public Task<MyClass> Foo()
{
return new Task<MyClass>(() =>
{
ReportProgress();
DoSubOperation1();
ReportProgress();
DoSubOperation2();
// ...
return myClass;
});
}
If you don't care about a return type, you can use the non-generic task. Then, when you call Foo(), you can call ContinueWith on the Task to give it a method to call once the Task completes.
If you want progress, you will need to keep your progress event because that's custom to the method that you're writing.
But overall, Task is a nice encapsulation for async functionality that keeps you from having to repeat the same MyMethod() MyMethodCompleted() pattern over and over again.
It's a reasonable idea, but I think it might be overkill, just because you're making assumptions that your events will be subscribed to. That said, I don't think it really causes too many problems, so long as your event subscribers behave properly; it's just that doing this incurs a little bit of overhead that may be wasted effort. That said, I don't really see anything wrong with it other than the (small) amount of (potentially unfruitful) time put into adding / raising the events.

Resources