c Winform cross thread operation controls

Updated on technology 2024-05-25
4 answers
  1. Anonymous users2024-02-11

    Controls created by the main thread cannot be used directly in a child thread. You need to establish a delegate, write the ** of the operation of the main thread control in the delegate event, and call the main thread control of the delegate operation in the child thread.

    Here's an example of writing::

    public partial class form1 : form

    thread thread=null;

    int n = 0;

    bool severisrun=false;It is used to show whether the thread is started or not.

    private delegate void mydelegate(string str);Define the delegation.

    private void setrich(string str) delegate.

    if (mydelegate md = new mydelegate(;

    new object

    str;public form1()

    initializecomponent();

    private void run()

    while(true)

    setrich((n++)tostring())

    private void button1_click(object sender, eventargs e)

    if(!severisrun)

    threadstart ts =new threadstart(run);

    thread = new thread(ts);

    severisrun=true;

    private void button2_click(object sender, eventargs e)

    if(severisrun)

    severisrun=false;

    private void form1_formclosing(object sender, formclosingeventargs e)

    if(severisrun)

    severisrun=false;

  2. Anonymous users2024-02-10

    Delegated. private void delegate changechkboxstatedelegate();

    If the private void changechkboxstate() method has a parameter, it must also have the same argument in the definition of the delegate above.

    if(changechkboxstatedelegate newdelegate = new changechkboxstatedelegate();

    new object{

  3. Anonymous users2024-02-09

    Yes, add it to the form creation or load method.

    false;

    You can call the feast across the Zhiling Silver Thread, Wang Chun. Generally, Mini Programs can do this by playing by themselves.

  4. Anonymous users2024-02-08

    UI elements in C are created by the main thread and can only be modified by the main thread that created them, because UI elements are not thread-safe by themselves, and multithreaded operations can cause unpredictable errors. So, by default, attempting to manipulate a UI element from a different thread throws an exception, although you can force cross-threaded operations of UI elements via the following:

    checkforillegalcrossthreadcalls = false;

    However, this is not recommended. In general, it is recommended for cross-threaded operations on UI elements.

    method);

    method, params object args);

    The invoke method can be called by any thread, but will only execute the corresponding delegate on the main thread that created the form.

    A minimal example is as follows:

    private void button1_click(object sender, eventargs e)

    private void threadaction()

    private void uiaction()

    Because the parameters of invoke include the delegate delegate, you need to define the corresponding delegate according to the parameters and return value of the method to invoke, and then you can call it. In the above example, the uiaction method has no parameters and no returns, so it directly uses the built-in delegate of methodinvoker.

Related questions
10 answers2024-05-25

Are you importing data from Excel to Winform? If so, it can be like this: after referencing the component, application; This is a client. >>>More

20 answers2024-05-25

SQL pagination bar uses a variable to record the number of pages into the database to check,Define three variables,The first is the current number of pages,The second one shows how many pieces of data per page,The third one looks up a count to show how many pages in total,The front is OK to bind with datasource。 >>>More

9 answers2024-05-25

It's a bit complicated, isn't it? The captcha mechanism of each ** is different, you don't have a clear goal, you can't tell you, and you don't give points, it's too stingy.

13 answers2024-05-25

I won't talk about how to review it specifically:

Let's first let you know the content of the graduate school entrance examination! I took the postgraduate examination this year and divided it into a preliminary examination and a re-examination, with a full score of 500 points in the preliminary examination English (100), mathematics (150), and politics (100) are national unified propositions, and there is also a professional course (150) with the proposition of the unit you are applying for. >>>More

4 answers2024-05-25

Hello, as a prospective graduate student who has come ashore with electronic information for the cross-examination in a certain Tiankeng this year, I hope to make you think about whether to cross the examination and what to do after deciding to cross the examination. >>>More