PDA

View Full Version : سوال فنی



alireza_tavakol
جمعه 06 مهر 1386, 01:35 صبح
بچه ها کی میدونه چرا توی کلاس Program.cs که توی همه برنامه های #C موجود است علت نوشتن خط زیر چیست
[STAThread]
چرا همین خط توی برنامه های #J نیست
و اگه من این خط رو پاک کنم بازم پروژه اجرا میشه پس می تونیم نتیجه بگیریم این خط اضافی
یا نه شایدم یه کمی تاثیر منطقی داره؟

ghafoori
جمعه 06 مهر 1386, 15:45 عصر
دوست عزیز اینها کدهای attribute هستند داخل همه زبانهای دات نت است مثلا داخل وی بی این طور می شود

<STAThread>
این کدها برای ادیتور بکار می رود همون ویژوال استدیو چون توضیحاتش زیاده این مطلب را بخوان
http://barnamenevis.org/forum/showthread.php?t=73558&highlight=attribute

alireza_tavakol
شنبه 07 مهر 1386, 16:30 عصر
قفوری جان کجای VB یه همچین دستوری داریم که من ندیدم
دومندش داخل #J هم یه همچین دستوری نداریم
سومندش این لینکی که دادین داخلش جواب مناسب نیست
چارمندش من پرسیدم چه کاری انجام میده که جوابی ندادین
پنجمندش من میگم اگه این خط رو پاکشم بکنم برنامه بازم اجرامشه آیا تاثیری دارد وچرا این تاثیر پنهان است

PC2st
شنبه 07 مهر 1386, 19:57 عصر
قبلا در این باره بحث شده بود:
http://barnamenevis.org/forum/showthread.php?t=73991
http://barnamenevis.org/forum/showthread.php?t=36146

ghafoori
یک شنبه 08 مهر 1386, 09:08 صبح
دوست عزیز اولا داخل وی بی به این صورت است

Public Class mycliass
<STAThread()> _
Sub Main(ByVal args() As String)

End Sub

End Class

دومندش داخل جی شارپ به این صورت

/** @attribute STAThread)( */


برای سومندش و چهارمندش یک راهنمایی و اشنایی بود برای شما که خودتان می توانید برای ادامه توضیحات داخل سایت جستجو کنید یا به Msdn مراجعه کنید

برای پنجمندش من گفتم

این کدها برای ادیتور بکار می رود
بکار می رود بنابراین تاثیری در برنامه ندارد

alireza_tavakol
یک شنبه 08 مهر 1386, 16:31 عصر
قفوری جون دستت درد نکنه اما در #J این خط در بین /* */ اصلا کامپایل نمی شود و کامنت به حساب میآید مثل این که اصلا نوشته نشده
بعدشم شما میگی واسه ادیتور کاربرد دارد ولی من میگم این که کاربردی که بود و نبودش یکی بعدشم بگو واسه ادیتور چه کاربردی دارد
تازشم این کلاسی که واسه VB نوشتی من تا حالا ندیم توی برنامه های WindowsApplication
حالا از کجا آوردی من نمیدونم

alireza_tavakol
یک شنبه 08 مهر 1386, 16:36 عصر
PC2st.ir دوست عزیز و استاد محترم شما چند تا لینک واسم گذاشتید متشکرم اما
سوالم رو این طوری تغییر میدم
آقا من توی پروژهام از COM استفاده بوکنم یا نکنم در هر حالت وجود یا نبود این خط در روند اجرای برنامه تاثیری ندارد (البته ظاهرا)
و یا من در برنامه ام از چند تا ترد استفاده کردم اما باز هم وجود یا نبود این خط ظاهرا تاثیری ندارد حالا
میپرسم پس وجود این کد چیه؟؟

ghafoori
یک شنبه 08 مهر 1386, 17:05 عصر
این کدها را من مستقیما از msdn کپی کردم ببینبد

Imports System
Imports System.IO
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization

' This class is serializable and will have its OnDeserialization method
' called after each instance of this class is deserialized.
<Serializable()> Class Circle
Implements IDeserializationCallback
Private m_radius As Double

' To reduce the size of the serialization stream, the field below is
' not serialized. This field is calculated when an object is constructed
' or after an instance of this class is deserialized.
<NonSerialized()> Public m_area As Double

Public Sub New(ByVal radius As Double)
m_radius = radius
m_area = Math.PI * radius * radius
End Sub

Private Sub OnDeserialization(ByVal sender As Object) _
Implements IDeserializationCallback.OnDeserialization
' After being deserialized, initialize the m_area field
' using the deserialized m_radius value.
m_area = Math.PI * m_radius * m_radius
End Sub

Public Overrides Function ToString() As String
Return String.Format("radius={0}, area={1}", m_radius, m_area)
End Function
End Class


Class Class1
<STAThread()> Shared Sub Main()
Serialize()
Deserialize()
End Sub

Shared Sub Serialize()
Dim c As New Circle(10)
Console.WriteLine("Object being serialized: " + c.ToString())

' To serialize the Circle, you must first open a stream for
' writing. Use a file stream here.
Dim fs As New FileStream("DataFile.dat", FileMode.Create)

' Construct a BinaryFormatter and use it
' to serialize the data to the stream.
Dim formatter As New BinaryFormatter
Try
formatter.Serialize(fs, c)
Catch e As SerializationException
Console.WriteLine("Failed to serialize. Reason: " + e.Message)
Throw
Finally
fs.Close()
End Try
End Sub


Shared Sub Deserialize()
' Declare the Circle reference
Dim c As Circle = Nothing

' Open the file containing the data that you want to deserialize.
Dim fs As New FileStream("DataFile.dat", FileMode.Open)
Try
Dim formatter As New BinaryFormatter

' Deserialize the Circle from the file and
' assign the reference to the local variable.
c = CType(formatter.Deserialize(fs), Circle)
Catch e As SerializationException
Console.WriteLine("Failed to deserialize. Reason: " + e.Message)
Throw
Finally
fs.Close()
End Try

' To prove that the Circle deserialized correctly, display its area.
Console.WriteLine("Object being deserialized: " + c.ToString())
End Sub
End Class

این هم جی شارپ

using namespace System;
using namespace System::IO;
using namespace System::Collections;
using namespace System::Runtime::Serialization::Formatters::Binary ;
using namespace System::Runtime::Serialization;

// This class is serializable and will have its OnDeserialization method
// called after each instance of this class is deserialized.

[Serializable]
ref class Circle: public IDeserializationCallback
{
private:
Double m_radius;

public:

// To reduce the size of the serialization stream, the field below is
// not serialized. This field is calculated when an object is constructed
// or after an instance of this class is deserialized.

[NonSerialized]
Double m_area;
Circle( Double radius )
{
m_radius = radius;
m_area = Math::PI * radius * radius;
}

virtual void OnDeserialization( Object^ /*sender*/ )
{
// After being deserialized, initialize the m_area field
// using the deserialized m_radius value.
m_area = Math::PI * m_radius * m_radius;
}

virtual String^ ToString() override
{
return String::Format( "radius= {0}, area= {1}", m_radius, m_area );
}
};

void Serialize()
{
Circle^ c = gcnew Circle( 10 );
Console::WriteLine( "Object being serialized: {0}", c );

// To serialize the Circle, you must first open a stream for
// writing. We will use a file stream here.
FileStream^ fs = gcnew FileStream( "DataFile.dat",FileMode::Create );

// Construct a BinaryFormatter and use it to serialize the data to the stream.
BinaryFormatter^ formatter = gcnew BinaryFormatter;
try
{
formatter->Serialize( fs, c );
}
catch ( SerializationException^ e )
{
Console::WriteLine( "Failed to serialize. Reason: {0}", e->Message );
throw;
}
finally
{
fs->Close();
}
}

void Deserialize()
{
// Declare the Circle reference.
Circle^ c = nullptr;

// Open the file containing the data that we want to deserialize.
FileStream^ fs = gcnew FileStream( "DataFile.dat",FileMode::Open );
try
{
BinaryFormatter^ formatter = gcnew BinaryFormatter;

// Deserialize the Circle from the file and
// assign the reference to our local variable.
c = dynamic_cast<Circle^>(formatter->Deserialize( fs ));
}
catch ( SerializationException^ e )
{
Console::WriteLine( "Failed to deserialize. Reason: {0}", e->Message );
throw;
}
finally
{
fs->Close();
}

// To prove that the Circle deserialized correctly, display its area.
Console::WriteLine( "Object being deserialized: {0}", c );
}

[STAThread]
int main()
{
Serialize();
Deserialize();
}

PC2st
یک شنبه 08 مهر 1386, 17:31 عصر
آقا من توی پروژهام از COM استفاده بوکنم یا نکنم در هر حالت وجود یا نبود این خط در روند اجرای برنامه تاثیری ندارد (البته ظاهرا)
این صفت بر روی عملکرد برنامه هم تاثیر میگذاره، برای اینکه تاثیر این صفت رو روی برنامه متوجه شوید، یک OpenFileDialog رو به فرم اضافه کنید و در رویداد Load از فرم، کد زیر رو بنویسید:


openFileDialog1.ShowDialog();

برنامه رو اجرا کنید... مشکلی نخواهد بود.
اما حالا بیایید و صفت STAThread (که قبل از متد Main است) رو پاک کنید، و دوباره برنامه رو اجرا کنید... اجرا نخواهد شد، چون یک ارور دریافت خواهید کرد!

alireza_tavakol
دوشنبه 09 مهر 1386, 01:06 صبح
می تونی علتشو توضیح بدی
چون من از لینک های ارسال شده این طوری برداشت کردم که این خط وظیفه ای به این شکل ندارد
و توضیح فقوری به نظر چه طور بوده

PC2st
دوشنبه 09 مهر 1386, 05:27 صبح
چون من از لینک های ارسال شده این طوری برداشت کردم که این خط وظیفه ای به این شکل ندارد
همین وظیفه رو داره...
در صورت استفاده از صفت STAThread، مدل آپارتمانی تک ریسمانی (STA) برای thread اصلی تنظیم خواهد شد.

و توضیح فقوری به نظر چه طور بوده
گفته ایشان همش درست بود فقط نکته ی "بنابراین تاثیری در برنامه ندارد" زیاد صحیح نبود :-)