C # threads cannot open the window

zhaozj2021-02-08  222

Time: 2004/4/14 Author: Robert Reference: MSDN

Email: zsc771120@yahoo.com.cn Keywords: Thread Form Open Window Show Thread Invoke Purpose: Helping people who have troubled by threads

In C #, the main window has a main thread, the main thread generates sub-thread monitoring socket, and the subclinger will send an event to the main thread, create a window. The current situation is that the sub-thread can receive the data stream. The main window can receive an event sent from the sub-thread and can create a window. This window has a problem: window status is the same as the window of dead programs.

Development encounters a very difficult problem, find a solution. The taste of the program error process, gradually track the program execution process, each line of code is performed, blame, big day, get the ghost during the day. The main window adds a button, buttons The role is to execute the event, launcher, click the button, and the program correctly created a window. According to this test result, the code in the event handling does not have any problems. In the execution program, track, find an error process. I think the program No problem, there should be no mistake; but really error, the program must have a problem, what is the problem, there is no answer; you think of the previous high-quality quotations: Calculator program is so simple, do not worry about professional experts, write procedures When I go to the calculator, I know who the program is correct. It is a scorpion that I need to take out. Yeah, I can't find the answer, turn online, to the forum to find this error related information, time waste, the result is not very Ok, I didn't find the answer. After I chatted with Faust, I asked about this problem. He pointed out one of the two problems between the message loop and threads. On the way FAUST's idea to find the answer, soon found relevant message.

Unveiling the final resolution, the event is a synchronous process, that is, although the sub-thread triggers the main window event, the thread that is executed is still a sub-thread, creating a window from frm1 = new form (); form.show (); can execute However, it is impossible to receive the Windows Print () event, so there is no problem with the window, that is, there is no Drafter on the window above, so the window is like a dead window, and it is inverse. How to deal with the cause? In the thread Use delegatedefine delegateTest = new delegateDefine (this.m_from.eventfunction; this.m_from.invoke (delegatetest); you can perform the program normally. Solve the most important thing is Invoke, if you are interested, you can see Invoke introduction.

From the problem, there is a problem, it takes ten hours, too hard.

Attachment: asynchronous appraisal program design example

The following program code demonstrations. Net asynchronous programming, use simple categories to decompose some digital factors.

[C #]

Using system;

Using system.threading;

Using system.runtime.remoting;

Using system.runtime.remoting.Messaging;

// Create an asynchronous delegate.

Public Delegate Bool FactorizingasyncDelegate

Int FactorizAblenum,

Ref int primefactor1,

Ref int primefactor2;

// Create a class trata factorizers the number.

Public Class PrimeFactorizer

{

Public Bool Factorize

Int FactorizAblenum,

Ref int primefactor1,

Ref Int PrimeFactor2)

{

PrimeFactor1 = 1;

PrimeFactor2 = FactorizAblenum;

// Factorize Using A Low-Tech Approach.

For (int i = 2; i

IF (0 == (FactorizAblenum% i)))

{

PrimeFactor1 = i;

PrimeFactor2 = FactorizAblenum / i;

Break;

}

}

IF (1 == PrimeFactor1)

Return False;

Else

Return True;

}

}

// Class That Receives A Callback When The Results Are Available.

Public Class ProcessFactorizedNumber

{

Private Int _ulnumber;

Public ProcessFactorizedNumber (int Number)

{

_ULNUMBER = NUMBER;

}

// Note That The Qualifier is One-Way.

[OnewayATTRIBUTE ()]

Public Void FactorizedResults (IASYNCRESULT AR)

{

INT FACTOR1 = 0, Factor2 = 0;

// Extract the delegate from the asyncResult.

FactorizingasyncDelegate FD = (FactorizingasyncDelegate) ((asyncResult) ar) .asyncdelegate;

// Obtain the result.

FD.Endinvoke (Ref Factor1, Ref Factor2, Ar);

// Output the results.

Console.writeline ("on callback: Factors of {0}: {1} {2}",

_ULNUMBER, FACTOR1, FACTOR2;

}

}

// Class That Shows Variations of Using Asynchronous

Public Class Simple

{

// The Following Demonstrates The Asynchronous Pattern Using A Callback.

Public void factorizenumber1 ()

{

// The folload is the client code.

PrimeFactorizer Pf = New PrimeFactorizer ();

FactorizingasyncDelegate fd = new factorizingasyncdelegate (pfActorize);

INT factorizables = 1000589023, TEMP = 0;

// Create an instance of the class thing is going

// to be caled when the call completion.

ProcessFactorizedNumber FC = New ProcessFactorizedNumber (FactorizAblenum);

// define the asyncCallback delegate.

AsyncCallback CB = New AsyncCallback (fc.factorizedResults);

// you can use any any object as the state object.

Object State = new object ();

// asynchronously invoke the factorize method on pf.iasyncResult ar = fd.beginInvoke

FactorizAblenum,

Ref temp,

Ref temp,

CB,

State);

//

// Do some of the other.

//.

}

// The folowing demonstrates the asynchronous pattern using a beginivou, Followed by waiting with a time-out.

Public void factorizenumber2 ()

{

// The folload is the client code.

PrimeFactorizer Pf = New PrimeFactorizer ();

FactorizingasyncDelegate fd = new factorizingasyncdelegate (pfActorize);

INT factorizables = 1000589023, TEMP = 0;

// Create an instance of the class thing is going

// to be caled when the call completion.

ProcessFactorizedNumber FC = New ProcessFactorizedNumber (FactorizAblenum);

// define the asyncCallback delegate.

AsyncCallback CB =

New asyncCallback (fc.factorizedResults);

// you can use any any object as the state object.

Object State = new object ();

// asynchronously invoke the factoryize method on pf.

IASYNCRESULT AR = fd.beginInvoke

FactorizAblenum,

Ref temp,

Ref temp,

NULL,

NULL);

Ar.asyncwaithandle.waitone (10000, false);

IF (ar.iscompleted)

{

INT FACTOR1 = 0, Factor2 = 0;

// Obtain the result.

FD.Endinvoke (Ref Factor1, Ref Factor2, Ar);

// Output the results.

Console.writeline ("Sequential: Factors of {0}: {1} {2}",

FactorizAblenum, Factor1, Factor2;

}

}

Public static void main (string [] args)

{

Simple Simple = New Simple ();

Simple.Factorizenumber1 ();

Simple.Factorizenumber2 ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-3026.html

New Post(0)