Win7 C # WinForm Programming SaveFileDialog cannot eject save form

Source: Internet
Author: User

public void Resmsg ()
{
while (ISRECMSG)
{
Prepare an array to receive data from the service side
byte[] Msgrec = new byte[1024 * 1024 * 2];
Receives data from the server, this method also blocks the current thread and returns the length of the received data
int reclength = sokclient.receive (MSGREC);
Switch (msgrec[0])
{
Case 1://received a file
DialogResult reuslt = new SaveFileDialog ().   ShowDialog (); ---There is a problem here
if (reuslt = = System.Windows.Forms.DialogResult.OK)
{
using (FileStream fs = new FileStream (Savefiledialog1.filename, FileMode.Create))
{
Fs. Write (Msgrec, 1, recLength-1);
}
ShowMsg ("File Saved! "+ savefiledialog1.filename);
}
Break
Case 2://Receive string message
{
Convert data to a string (the array to convert, the subscript to start the conversion, the length of the data to be converted)
String Strrec = System.Text.Encoding.Default.GetString (Msgrec, 1, recLength-1);
Display the received data in a text box
ShowMsg (STRREC);
Break
}
Case 3://received a splash screen command
{
Shakewindow ();
Break
}
}
}
}

When the client and server transfer files, the server side shows that the send is successful, but when the client saves, the SaveFileDialog Save dialog box is not able to play???

Check the code carefully several times, logically without errors. Just can't play out, very silent. On the internet, looked up a lot of information, there is no clue. In the course of the investigation, I saw such a sentence "You said before, you use the thread call, then this thread is the main path?" If it is not the main thread, then you should give it to the main thread to handle ", and then I use an example to simulate a bit, it does have a relationship.

Examples of simulations:

public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}

private void Button1_Click (object sender, EventArgs e)
{
Thread t = new thread (savedialog);
T.isbackground = true;
t.apartmentstate = apartmentstate.sta;//A lack of this sentence will make a mistake.

However, the above sentence is shown in VS 2010 is rejected by Microsoft, outdated, although still available. But for a better compatible version, Microsoft introduced the following methods:

    t.setapartmentstate (apartmentstate.sta); That is, set its cell state when the thread starts, and set its state to a single-threaded unit


T.start ();
}

public void Savedialog ()
{
SaveFileDialog result = new SaveFileDialog ();
Result. ShowDialog ();
}
}

Example of simulation: the lack of this phrase "t.apartmentstate = ApartmentState.STA"will make an error. However, the setting in VS2010 is changed to t.setapartmentstate (ApartmentState.STA);//is the unit state that is set when the thread is started, where it is set to a single-threaded unit

Similarly savefiledialog Save dialog box Just can't play??? Is that the problem? After trying, it really is the problem.

So simple a sentence, it took me two hours, but finally resolved, but also very happy, and in the process of finding a lot of harvest. This is the loss must have! Come on!!!

This article reprinted to http://daociyiyou111.blog.163.com/blog/static/164701512201182104248626/

But I made a slight change.

Win7 C # WinForm Programming SaveFileDialog cannot eject save form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.