C How to prevent the interface from freezing the interface when executing time consuming operations

Updated on technology 2024-02-25
12 answers
  1. Anonymous users2024-02-06

    If the interface is unresponsive, it means that your ** has been in the UI thread, and before the end of execution, the UI thread can't do anything else, and it will naturally be unresponsive.

    If you're sure it's your loadinghandle that's taking too long to execute, you can put it in a background thread to do it

    await ;

    This is followed by your update of the UI.

  2. Anonymous users2024-02-05

    To put it simply, the thread is not directly related to the delegate, and the sub-thread can be opened in the main thread (UI thread is the background **) to prevent the interface from getting stuck, as for the delegate, it generally only needs to update the UI in the sub-thread before the invoke delegate is needed. You can take a look. The above action(() is simply written and does not require a delegation.

  3. Anonymous users2024-02-04

    backgoundworker is multi-threaded, which can generally be solved.

    worker = new backgroundworker();

    new doworkeventhandler(worker_dowork);

    new runworkercompletedeventhandler(worker_runworkercompleted);

    There are two ways to do it, namely:

    private void worker_dowork(object sender, doworkeventargs e)

    Used to handle the work that needs to be done, this is done in another thread.

    private void worker_runworkercompleted(object sender, runworkercompletedeventargs e)

    This is used to process the actions that will be responded to in the interface after the process is completed.

    If the interface is stuck, you can only check where the breakpoint time is.

  4. Anonymous users2024-02-03

    The program's feign of death and reduced response time are two concepts that are handled differently.

    1. There is generally only one reason for suspended animation, that is, the interface thread is blocked.

    By default, WPF and WinForm applications have only one thread, that is, UI threads. When you write some time-consuming ** to events, then the UI thread will be blocked, and the user will be reflected in "the interface is stuck".

    The solution is simple, use background threads to handle time-consuming tasks and keep UI threads flowing.

    Example: void button1 click(object sender, routedeventargs e).

    var thread = new => )

    2. Reduce response time.

    This is too broad and difficult to answer specifically, the general business system will start from two aspects, one is to optimize from **, reduce redundancy**, reduce multiple interactive operations in the same event, avoid multiple queries and so on.

    The second is from the database optimization, which depends on the ability of the database engineer. Simple systems can be indexed and read/write splitting, while more complex systems may require database engineers to build better architectures.

  5. Anonymous users2024-02-02

    Multithreading mainly deals with this; thread

  6. Anonymous users2024-02-01

    The solution to this problem can be done using multithreading.

    For example, the landlord puts a lable control in the form, writes a for loop, and assigns the value of the lable from 0, adding 1 to the value every second. But no matter how good your logic is, as long as you put this ** on the main thread, then the form will appear in a suspended animation state, and it will not resume until the loop is completed, and in the end you can only see the last value of lable.

    The method that the child thread will execute.

    public void a()

    Declare child threads.

    thread t = new thread(new threadstart(a));When declaring a child thread, the method it executes must be parameter-free and free of parentheses.

    The sub-thread begins.

    According to the landlord's situation, the landlord can see a() as a way to change the strb value, declare the sub-thread, start the sub-thread, and write it in the button click event.

    However, it should be noted that if the landlord changes the controls of the main thread in the child thread, an error may be reported during compilation. That's why delegates are used. Write an example to the landlord**.

    Define a delegate that changes the properties of a control. Global variables.

    private delegate void delegate1();

    private delegate1 d1;

    Define subthreads for method execution.

    private thread t;

    Define a string that records the display value of the lable control. Global variables.

    private string state = "Debugging";

    Defines a Boolean value that records the state of the child thread. Global variables.

    private bool isstart = false;

    Used to display the status.

    public void showstatedelegate()

    public void showstate()

    if(!isstart)

    button click event.

    d1 = new delegate1(showstatedelegate);

    t = new thread(new threadstart(showstate));

    Purely handwritten**, there may be mistakes, but the general logic is like this.

  7. Anonymous users2024-01-31

    Here's how:

    Please wait for ......";

    funclongtask = new func(delegate()) to make an asynchronous call, which is actually in. Execute longtask in the .net thread pool

    At this time, since other threads are working, the UI thread is not blocked, so the form will not feign = >

    null);

  8. Anonymous users2024-01-30

    This is because button1 click(..When the method is processed, the UI cannot be redrawn (the UI redraw and the button1 click method will be called in the same thread), so the interface freezes.

    The solution is to put the entire while loop into an asynchronous process (the current while loop is not an asynchronous call).

  9. Anonymous users2024-01-29

    t[i].join(5000);

    Isn't this the main thread blocked by the subject himself???

    Each sub-thread written by the subject tells the main thread: "You stop me for five seconds", and now asks why the main thread faked his death???

    Moreover, the lock (new object()) sentence has no purpose other than to increase the burden on the CPU, and it feels like the subject wants to add locks, but in fact, each thread locks different objects, which does not reflect the meaning of locks at all.

  10. Anonymous users2024-01-28

    How much memory do you have, add it to 8G and try it.

  11. Anonymous users2024-01-27

    I don't know the specific reason.,But there's no point in your timer here.,Just use the loop directly.,Similar to the following.,Timer2 or something** can be deleted.,Start the following thread directly when formload.。

    new thread((threadstart)(delegate);;

    start();

  12. Anonymous users2024-01-26

    Add a sentence to the subthread; Give it a try.

Related questions
4 answers2024-02-25

For example, if you want to modify a collection, but the collection may have multiple threads accessing it. Threads make different changes to the collection, which can lead to unstable collection states. So, when making changes to a collection, give the thread a lock, lock it, modify the collection, and then release the lock so that other processes can continue to access the collection without problems. >>>More

2 answers2024-02-25

1. MySQL database has several configuration options that can help us capture inefficient SQL statements in a timely manner1, Slow Query Log >>>More

11 answers2024-02-25

In C++, vector is used as a container, and its function is to store variables of struct type. Here's how vector might be used: >>>More

11 answers2024-02-25

It's textbox, right? textbook?

double somedouble; >>>More

6 answers2024-02-25

Understand the following rules: 1) Overloading an operator does not change the priority of the operator. >>>More